使用与根位置不同的路径提供 Wordpress 静态服务

使用与根位置不同的路径提供 Wordpress 静态服务

我的 Wordpress 应用程序运行于https://example.com/wordpress/myapp. 我已经这样设置了 nginx:

#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;

}

我的项目根目录为:

/var/www/webroot/wordpress

我可以访问应用程序并浏览所有链接,但图像、javascript 和样式表均未加载。它们全部返回未找到的 Wordpress 页面,好像是 php 页面,而不是静态文件。

错误日志

2017/08/11 00:54:20 [错误] 8085#0:* 290 FastCGI 在 stderr 中发送:“无法打开主脚本:/var/www/webroot/wordpress/myapp/wp-cron.php(没有此文件或目录)”从上游读取响应标头时,客户端:10.70.28.156,服务器:localhost,请求:“POST /wordpress/myapp/wp-cron.php?doing_wp_cron=1502412860.0307459831237792968750 HTTP / 1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“gabaquest.jelasticlw.com.br”,引荐来源:“http://example.com/wordpress/myapp/wp-cron.php?doing_wp_cron=1502412860.0307459831237792968750

php-fpm.log

[2017 年 8 月 11 日 00:51:59] 警告:[pool www] 子进程 8075 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/wordpress/myapp/wp-cron.php(没有此文件或目录)” [2017 年 8 月 11 日 00:52:17] 警告:[pool www] 子进程 8082 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/xmlrpc.php(没有此文件或目录)” [2017 年 8 月 11 日 00:52:17] 警告:[pool www] 子进程 8083 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/wp-login.php(没有此文件或目录)” [2017 年 8 月 11 日00:52:18] 警告:[pool www] 子进程 8033 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/wp-login.php(没有此文件或目录)” [2017 年 8 月 11 日 00:52:18] 警告:[pool www] 子进程 8035 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/wp-login.php(没有此文件或目录)” [2017 年 8 月 11 日 00:52:18] 警告:[pool www] 子进程 8034 在 stderr 中发出:“错误:无法打开主脚本:/var/www/webroot/wp-login.php(没有此文件或目录)” [2017 年 8 月 11 日 00:54:20] 警告:[pool www] 子进程8037 在 stderr 中显示:“错误:无法打开主脚本:/var/www/webroot/wordpress/myapp/wp-cron.php(没有此文件或目录)”

php_errors.log

[2017 年 8 月 11 日 00:52:17 UTC] PHP 已弃用:自动填充 $HTTP_RAW_POST_DATA 已弃用,并将在未来版本中删除。要避免出现此警告,请在 php.ini 中将“always_populate_raw_post_data”设置为“-1”,并改用 php://input 流。位于 Unknown 的第 0 行

答案1

看起来您在尝试修复配置时已经破坏了不少配置。我已尽力将其恢复到正常工作状态。

将来,请确保在常规非正则表达式位置块的末尾有一个尾部斜杠。Nginx 指令对尾部斜杠很敏感;只需添加或删除斜杠,就可以完全改变指令的行为。

从技术上讲,处理这种情况的正确方法是使用别名指令,但它与许多流行的 Nginx 功能(包括 fastcgi 模块)结合使用时,漏洞非常多。因此,我选择了重写路线。这不会导致重定向;重写是内部的。

#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;
}

相关内容