0

I am quite new to Nginx and I just can't make it work. I have a web app hosted in Azure. It's a WordPress website.

Here is the Nginx file:

server {
    #proxy_cache cache;
        #proxy_cache_valid 200 1s;

    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php;

    server_name  my-app.azurewebsites.net; 
    port_in_redirect off;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
        sendfile off;
        set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
                set $skip_cache 1;
        }

        if ($query_string != "") {
                set $skip_cache 1;
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                set $skip_cache 1;
        }

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
                set $skip_cache 1;
        }

        # Don't cache WooCommerce URLs
        # Cart widgets are still a problem: https://github.com/emcniece/docker-wordpress/issues/3
        if ($request_uri ~* "/(cart|checkout|my-account)/*$") {
                set $skip_cache 1;
        }

    location / {            
        try_files $uri $uri/ /index.php?$args;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /html/;
    }
    
    # Disable .git directory
    location ~ /\.git {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Add locations of phpmyadmin here.
#    location ~ [^/]\.php(/|$) {
    location ~ \.php$ {
#        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass 127.0.0.1:9000;
#       fastcgi_pass php;

        include fastcgi_params;

        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;

        fastcgi_intercept_errors on;
#        fastcgi_connect_timeout         300; 
#        fastcgi_send_timeout           3600; 
#        fastcgi_read_timeout           3600;
#        fastcgi_buffer_size 128k;
#        fastcgi_buffers 4 256k;
#        fastcgi_busy_buffers_size 256k;
#        fastcgi_temp_file_write_size 256k;

        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache off;
        fastcgi_cache_valid 60m;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

    add_header "Access-Control-Allow-Origin"  *;
}

server {
    listen 80;
    server_name my-app.azurewebsites.net;
    return 301 https://$host$request_uri;
}
  1. My first issue is related to the headers. I tried to add some headers but they are completely ignored. I searched through the parent block, hoping to find something that is overwriting it, in the main Nginx folder, but haven't found anything related to headers.

  2. Another issue is related to /wp-admin folder which I can't access. It enters in some loop with response code 302.

  3. I don't know why but some resources are loaded via http and some others via https.

Any suggestions would much appreciated.

Thanks in advance, Ciprian

pictoru
  • 103
  • 3

1 Answers1

0

Your biggest issue here is that your config file is full of cut and pasted directives that would make it impossible to debug. e.g. why are you disabling sendfile? You seem to have lots of config for bypassing a local cache, but no local cache defined. Please learn how to use the include directive to make this more manageable and get rid of the redundant stuff.

I tried to add some headers but they are completely ignored

You didn't tell us what you tried. IME ngx_http_headers_module comes as standard in most distributions of nginx and add_header works exactly like its supposed to.

Another issue is related to /wp-admin folder which I can't access. It enters in some loop with response code 302.

Probably redirecting to the URL you just requested? (it would have been helpful if you had provided the URL you were requesting and respose headers). That's because wp_admin checks to see if the incoming connection is over https. Yours is not. Your SSL appears to be terminated on a reverse proxy (again, my crystal ball is working overtime). A quick Google turned up https://wordpress.stackexchange.com/questions/387990/how-do-i-handle-ssl-properly-when-wp-is-behind-a-reverse-proxy and http://www.michaelfoster82.co.uk/how-to-set-up-wordpress-behind-a-secure-reverse-proxy-using-nginx/

I don't know why but some resources are loaded via http and some others via https

Sloppy programming. But making wordpress believe that the incoming connection is over HTTPS (see above) should fix.

symcbean
  • 21,009
  • 1
  • 31
  • 52