Php 当使用NGINX作为apache的反向代理时,Wordpress Permalinks返回404

Php 当使用NGINX作为apache的反向代理时,Wordpress Permalinks返回404,php,wordpress,apache,nginx,nginx-reverse-proxy,Php,Wordpress,Apache,Nginx,Nginx Reverse Proxy,我正试图让NGINX反向代理并为运行在Apache端口8086上的WordPress站点提供SSL终止。我希望NGINX处理静态文件,并且只代理对Apache的PHP请求 我已经成功地让这个工作使用标准链接。 (即工作正常) 当我启用任何类型的永久链接时,主页将加载,wp admin也会加载,但失败 看看NGINX日志,我明白了 2018/05/23 09:36:40 [error] 7472#0: *1 "/var/www/example.com/live_site/what-we-do/in

我正试图让NGINX反向代理并为运行在Apache端口8086上的WordPress站点提供SSL终止。我希望NGINX处理静态文件,并且只代理对Apache的PHP请求

我已经成功地让这个工作使用标准链接。 (即工作正常)

当我启用任何类型的永久链接时,主页将加载,wp admin也会加载,但失败

看看NGINX日志,我明白了

2018/05/23 09:36:40 [error] 7472#0: *1 "/var/www/example.com/live_site/what-we-do/index.php" is not found (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: example.com, request: "GET /what-we-do/ HTTP/2.0", host: "example.com", referrer: "https://example.com/?post=274"
因此,NGINX试图将/permalink/index.php作为静态路径/文件来查找,而不是传递给apache。你有没有想过如何让它发挥作用

我的NGINX配置如下所示:

upstream example_apache {
    ip_hash;
    server 127.0.0.1:8086;
}

server {
# HTTP/HTTPS Server Block
# General Config
    listen                      [::]:80;
    listen                      80;
    listen                      [::]:443 http2 ssl;
    listen                      443 http2 ssl;
    server_name                 example.com
                                www.example.com;

    root                        /var/www/example.com/live_site;
    access_log                  /var/log/nginx/access-example.com.log main;
    error_log                   /var/log/nginx/error-example.com.log;
    index                       index.php;

#SSL Cert Configuration
# Check SSL config at https://www.ssllabs.com/ssltest/
    ssl_prefer_server_ciphers   on;
    ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                 "ECDHE-ECDSA-CHACHA20-POLY1305 ECDHE-RSA-CHACHA20-POLY1305 EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH DHE-RSA-CHACHA20-POLY1305 EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4 !SEED !CAMELLIA";
    ssl_session_cache           shared:SSL:100m;
    ssl_session_timeout         180m;
    ssl_dhparam                 /var/www/certs/dh4096.pem;

    ssl_certificate             /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.secp384r1.cer;
    ssl_certificate_key         /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.secp384r1.key;
    ssl_certificate             /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.rsa4096.cer;
    ssl_certificate_key         /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.rsa4096.key;

# Enable HSTS #Deploy in stages to prevent extended loss to site.
    add_header                  Strict-Transport-Security "max-age=300; includeSubdomains;"; #300s-5min TTL Testing
    #add_header                 Strict-Transport-Security "max-age=604800; includeSubdomains;"; #1week TTL Testing
    #add_header                 Strict-Transport-Security "max-age=2592000; includeSubdomains;"; #1month TTL Testing
    #add_header                 Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"; #10886400s-126days Min for Preload
    #add_header                 Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"; #63072000s-2years Production Value

# OCSP Configuration
    ssl_trusted_certificate     /var/www/certs/lets_encrypt/web01.example.com/web01.example.com.fullchain.secp384r1.cer;
    ssl_stapling                on;
    ssl_stapling_verify         on;
    resolver                    8.8.4.4 8.8.8.8 valid=300s;
    resolver_timeout            10s;

# LetEncrypt webroot alias
    location /.well-known/acme-challenge/ {
        alias /var/www/le_root/.well-known/acme-challenge/;
    }
# www to non-www rewrite
# Redirect to the correct place, if needed
    set $https_redirect 0;
    if ($server_port = 80) { set $https_redirect 1; }
    if ($host ~ '^www\.') { set $https_redirect 1; }
    if ($https_redirect = 1) {
        return 301 https://example.com$request_uri;
    }

# Wordpress entry point
    location / {
        #Try                    file dir    index.php else 404
        try_files               $uri $uri/ /index.php?$args =404;

        #All Files except for *.php
        location ~ .+(?<!\.php)$ {
            location ~ ^[^.]+\.[^.]+$ {
                expires         max;
                add_header      Cache-Control public;
                break;
            }
        }

        #Only *.php files
        location ~ \.php$ {
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    Host                $host;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto   $scheme;
            proxy_pass_header                       Set-Cookie;

            proxy_set_header    SSL_PROTOCOL        $ssl_protocol;
            proxy_set_header    SSL_CLIENT_CERT     $ssl_client_cert;
            proxy_set_header    SSL_CLIENT_VERIFY   $ssl_client_verify;
            proxy_set_header    SSL_SERVER_S_DN     $ssl_client_s_dn;

            proxy_pass                              http://example_apache;
        }
    }
}
我的apache配置有:

<VirtualHost *:8086>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com/live_site
ServerName  example.com
ServerAlias www.example.com


ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined

Alias "/.well-known/acme-challenge/" "/var/www/le_root/.well-known/acme-challenge/"

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/example.com/live_site>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

</VirtualHost>

服务器管理员webmaster@localhost
DocumentRoot/var/www/example.com/live_site
ServerName example.com
ServerAlias www.example.com
ErrorLog${APACHE_LOG_DIR}/example.com.error.LOG
CustomLog${APACHE_LOG_DIR}/example.com.access.LOG组合
别名“/.well-known/acme challenge/”/var/www/le_root/.well-known/acme challenge/”
选项如下符号链接
不允许超限
选项索引跟随符号链接多视图
允许超越所有
命令允许,拒绝
通融
我还应该注意,当我直接连接到Apache时,我可以正确地看到所有页面永久链接。(即工作正常)

NGINX版本1.13.9
Apache 2.4.33 mpm_prefork
PHP 7.1版


如果您有任何想法或帮助NGINX将permalinks正确代理到apache,我们将不胜感激

启用或检查是否使用此命令启用mod_rewrite


sudoa2enmod rewrite

在使用NGINX将Prod环境迁移到Docker时,我收到了相同的错误,但我没有对Apache进行反向代理。不过,我的错误是一样的

原因是我必须更改
wp\u选项
,以匹配新的本地端口和URL

从wp_选项中选择*选项,其中选项_name='siteurl'或选项_name='home'
将显示WordPress配置试图导航到的当前URL。但是,由于您已经创建了一个代理,现在您的WordPress站点位于不同的端口或URL后面,因此您可能需要更改这些值

当您执行该命令时,您将收到站点用作前缀的两个URL的列表。如果它显示代理的URL,那么这可能不起作用

然后我修改了URL以匹配新后端URL+端口的位置。在您的情况下,您可能需要更改它以匹配代理后面的端口和url,而不是代理本身的url

在my
wp config.php
中修改这些值无效。e、 g

 define('WP_HOME','http://local.www.greenhousetreatment.com:8080');
 define('WP_SITEURL','http://local.www.greenhousetreatment.com:8080');
这对我不起作用

我必须在SQL中手动使用上面的命令,然后更新这些值以匹配网站的端口和URL。通常在反向代理中,您将键入代理URL,然后它将命中您的服务IP和端口。您的服务IP和端口做了它需要做的事情,因为它根本不关心代理。它甚至不知道代理的事

您确定您的
wp\u选项
与实际的服务URL和端口匹配,而不是与代理URL匹配吗


我希望这能给我们一些启示

正如我所说,当我访问由apache提供服务的站点时,permalinks就开始工作了。只有当nginx代理时,它们才能正确通过。如果我删除nginx代理并让apache直接服务,那么这个站点已经100%工作了。遗憾的是,没有人知道这里需要做什么。我曾经多次遇到过这个问题,这让我想知道反向代理Wordpress的好处是否值得。不知道如果我用Varnish而不是NGINX,我是否也会遇到同样的问题。我只是想在这里继续,这没有帮助。问题发生在wordpress介入之前,正如您在上面看到的,NGINX试图在错误的位置查找index.php。我猜我可能不得不在NGINX中加入一些复杂的重写规则。我最终放弃了它,因为它花费了太长的时间来正确配置。我让NGINX直接为wordpress服务。
<VirtualHost *:8086>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com/live_site
ServerName  example.com
ServerAlias www.example.com


ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined

Alias "/.well-known/acme-challenge/" "/var/www/le_root/.well-known/acme-challenge/"

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/example.com/live_site>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

</VirtualHost>
 define('WP_HOME','http://local.www.greenhousetreatment.com:8080');
 define('WP_SITEURL','http://local.www.greenhousetreatment.com:8080');