0

My Wordpress application run on https://example.com/wordpress/myapp. I've set nginx like this:

#user  nobody;
worker_processes  1;


error_log  /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    #allows file uploads up to 8 megabytes
    client_max_body_size 50M;

    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    client_header_buffer_size 4k;
    large_client_header_buffers 4 16k;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Websocket support
    #upstream websocket {
    #    server 127.0.0.1:<PORT>;
    #}

    server {
        listen       80;
        server_name  localhost;

        #cookies default descomplica
        client_header_buffer_size 4k;
        large_client_header_buffers 4 16k;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        include /etc/nginx/aliases.conf;

        #location /ws {
        #    proxy_pass http://websocket;
        #    proxy_http_version 1.1;
        #    proxy_set_header Upgrade $http_upgrade;
        #    proxy_set_header Connection "Upgrade";
        #}

        location /wordpress/myapp {
            root   /var/www/webroot;
            index  index.php /wordpress/index.php;
            try_files $uri $uri/ /wordpress/index.php?$args;

            location ~ \.php$ {
              location ~ /\. { deny all; access_log off; log_not_found off; }
              include /etc/nginx/fastcgi_params;
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
              fastcgi_param DOCUMENT_ROOT $realpath_root;
              fastcgi_param PATH_INFO $fastcgi_script_name;
            }
        }

        index  index.php index.html index.htm;

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    location ~ /\. { deny all; access_log off; log_not_found off; }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    location ~ \.php$ {
        location ~ /\. { deny all; access_log off; log_not_found off; }
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT /var/www/webroot;
        }

    }

include /etc/nginx/conf.d/*.conf;

}

My project root is at:

/var/www/webroot/wordpress

I can access the application and navigate through all links, but none of images, javascripts and stylesheets are loaded. All of them return the not found page of Wordpress, as if was a php page, not a static file.

error.log

2017/08/11 00:54:20 [error] 8085#0: *290 FastCGI sent in stderr: "Unable to open primary script: /var/www/webroot/wordpress/myapp/wp-cron.php (No such file or directory)" while reading response header from upstream, client: 10.70.28.156, server: localhost, request: "POST /wordpress/myapp/wp-cron.php?doing_wp_cron=1502412860.0307459831237792968750 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "gabaquest.jelasticlw.com.br", referrer: "http://example.com/wordpress/myapp/wp-cron.php?doing_wp_cron=1502412860.0307459831237792968750"

php-fpm.log

[11-Aug-2017 00:51:59] WARNING: [pool www] child 8075 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wordpress/myapp/wp-cron.php (No such file or directory)" [11-Aug-2017 00:52:17] WARNING: [pool www] child 8082 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/xmlrpc.php (No such file or directory)" [11-Aug-2017 00:52:17] WARNING: [pool www] child 8083 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wp-login.php (No such file or directory)" [11-Aug-2017 00:52:18] WARNING: [pool www] child 8033 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wp-login.php (No such file or directory)" [11-Aug-2017 00:52:18] WARNING: [pool www] child 8035 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wp-login.php (No such file or directory)" [11-Aug-2017 00:52:18] WARNING: [pool www] child 8034 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wp-login.php (No such file or directory)" [11-Aug-2017 00:54:20] WARNING: [pool www] child 8037 said into stderr: "ERROR: Unable to open primary script: /var/www/webroot/wordpress/myapp/wp-cron.php (No such file or directory)"

php_errors.log

[11-Aug-2017 00:52:17 UTC] PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

  • Question is confusing. Is the wordpress home page example.com/wordpress/myapp/index.php or example.com/wordpress/index.php? Where's the rest of your Nginx configuration that tells Nginx how to load static resources? Is having the PHP location nested inside the /wordpress/myapp location deliberate or an accident? That last part is valid but not how I tend to do things. – Tim Aug 10 '17 at 23:50
  • The home of the wordpress is example.com/wordpress/myapp/index.php, but the the code root is /wordpress. I updated with the full nginx.conf – Ibn Claudius Aug 11 '17 at 0:08
  • I assume you've checked the Nginx and PHP logs for information? The Nginx access log should tell you what file it's trying to load. Please edit your question to include them. You really should provide as much information as possible, rather than having to explicitly ask for information bit by bit. – Tim Aug 11 '17 at 0:47
  • Sorry. I've edited it again with the logs. I can't see anything related to my problem on the logs, I'm not familiar with nginx. – Ibn Claudius Aug 11 '17 at 0:59
  • Those logs are irrelevant to your problem. You need to do a curl then look at the Nginx access / error logs. Mine are in /var/log/nginx , do "ls -ltr" to see the most recently changed files at the bottom. – Tim Aug 11 '17 at 1:00
0

It looks like you've broken the config quite a bit trying to fix it. I've done my best to restore it to a working state.

In the future, make sure you have a trailing slash at the end of your normal, non-regex location blocks. Nginx directives are sensitive to trailing slashes; you can completely change the behavior of a directive just by adding or removing a slash.

Technically, the correct way to handle this situation would be to use the alias directive, but it's extremely buggy in combination with a number of popular Nginx features, including the fastcgi module. Instead, I took the rewrite route. This won't cause a redirect; the rewrite is internal.

#user  nobody;
worker_processes  1;

error_log   /var/log/nginx/error.log;
#error_log  /var/log/nginx/notice.log  notice;
#error_log  /var/log/nginx/info.log    info;

events {
    worker_connections  1024;
}


http {
    #allows file uploads up to 50 megabytes
    client_max_body_size 50M;

    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    client_header_buffer_size 4k;
    large_client_header_buffers 4 16k;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        root /var/www/webroot;
        index index.php index.html index.htm;

        #cookies default descomplica
        client_header_buffer_size 4k;
        large_client_header_buffers 4 16k;

        #charset koi8-r;

        # TODO: This may need to be removed.  This could be part of the problem.
        include /etc/nginx/aliases.conf;

        location ~ /\. { return 404; }

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            fastcgi_param PATH_INFO $fastcgi_script_name;
        }

        location ^~ /wordpress/myapp/ {
            # We could use the `alias` directive instead, but that tends to break in combination with the fastcgi module.
            rewrite ^/wordpress/myapp(/.*)$ $1;
        }

        location /wordpress/ {
            # If you want to prevent people from visiting /wordpress/ directly and require that they visit /wordpress/myapp/, you can try uncommenting the next line.  You'll also need to duplicate the PHP location block, change the regex in the new one to `^/wordpress/.*\.php$`, and add the `internal` directive to that, too.
            #internal;
            try_files $uri $uri/ /wordpress/index.php;  # This PHP file has to come last.  It's deliberately missing `myapp`.
        }

        location / {
            try_files $uri $uri/ =404;
        }

        # TODO: This either needs to be `location = /ws` or `location ^~ /ws/`.  Note the trailing slash.
        #location /ws {
        #    proxy_pass http://websocket;
        #    proxy_http_version 1.1;
        #    proxy_set_header Upgrade $http_upgrade;
        #    proxy_set_header Connection "Upgrade";
        #}
    }

    include /etc/nginx/conf.d/*.conf;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.