Nginx反向代理与Laravel的WordPress

我目前正在pipe理一个设置,在domain.com上使用Laravel webapp并在domain.com/blog上运行一个Wordpress博客。

domain.com/blogpath代理到Wordpress博客所在的另一台服务器。

build立

服务器1

基于Laravel运行webapp的nginx webserver:

server { listen 80; server_name default.com; return 301 https://www.default.com$request_uri; } server { listen 443; ssl_certificate /etc/letsencrypt/live/www.default.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.default.com/privkey.pem; server_name default.com; return 301 https://www.default.com$request_uri; } server { listen 443 ssl; listen [::]:443 ssl ipv6only=on; root /var/www/html/default/current/public; index index.php index.html index.htm; server_name www.default.com; error_log /var/log/nginx/www.default.com.error.log debug; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } location /blog/ { proxy_pass http://10.2.7.3/blog/; proxy_set_header Host $host; } ssl_certificate /etc/letsencrypt/live/www.default.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.default.com/privkey.pem; } 

服务器2

运行WordPressApache webserver:

 <IfModule mod_ssl.c> <VirtualHost _default_:443> ServerAdmin webmaster@default.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key </VirtualHost> </IfModule> 

服务器2上的目录结构:/ var / www / html / var / www / html / blog < – 这里是WordPress的博客

WordPress .htaccess文件:

 RewriteEngine On RewriteBase /blog RedirectMatch 301 ^/blog/author/ https://www.default.com/blog RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] 

问题博客本身工作正常,所有页面都可见,但wp-admin /redirect到wp-login.php失败。

CURL请求wp-admin /

 curl https://www.default.com/blog/wp-admin/ < HTTP/1.1 302 Moved Temporarily < Server: nginx/1.10.1 < Content-Type: text/html; charset=UTF-8 < Location: https://www.default.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fwww.default.com%2Fblog%2Fblog%2Fwp-admin%2F&reauth=1 

好的,让我们按照这个redirect

 curl -v 'https://www.default.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fwww.default.com%2Fblog%2Fblog%2Fwp-admin%2F&reauth=1' 

这个调用现在由Laravelnetworking应用程序处理,而不是Wordpress博客。 这不是应该发生的事情。 这是由nginxconfiguration引起的:

 location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

为什么这个configuration覆盖了proxy_pass?

nginx根据文档解释的各种规则评估location块。

要给你的location /blog/location ~ \.php$更高的优先级,使用^~修饰符:

 location ^~ /blog { proxy_pass http://10.2.7.3; proxy_set_header Host $host; }