Nginx 反向代理到 Wordpress Laravel
Nginx reverse proxy to Wordpress with Laravel
我目前正在管理一个设置,我们在 domain.com 上使用 Laravel 网络应用程序,在域上使用 运行 Wordpress 博客。com/blog.
域。com/blog 路径代理到 Wordpress 博客所在的另一台服务器。
设置
服务器 1
nginx webserver 运行 webapp 基于 Laravel:
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
Apache 网络服务器运行Wordpress:
<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/ 重定向到 wp-login.php 失败。
对 wp-admin/
的 CURL 请求
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
好的,让我们跟随那个重定向
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'
此调用现在由 Laravel 网络应用而非 Wordpress 博客处理。那不是应该发生的事情。
这是由 nginx 配置引起的:
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;
}
为什么那部分配置会覆盖 proxy_pass?
nginx
根据各种规则评估 location
个块,如 explained by the documentation。
要使 location /blog/
的优先级高于 location ~ \.php$
,请使用 ^~
修饰符:
location ^~ /blog {
proxy_pass http://10.2.7.3;
proxy_set_header Host $host;
}
我目前正在管理一个设置,我们在 domain.com 上使用 Laravel 网络应用程序,在域上使用 运行 Wordpress 博客。com/blog.
域。com/blog 路径代理到 Wordpress 博客所在的另一台服务器。
设置
服务器 1
nginx webserver 运行 webapp 基于 Laravel:
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
Apache 网络服务器运行Wordpress:
<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/ 重定向到 wp-login.php 失败。
对 wp-admin/
的 CURL 请求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
好的,让我们跟随那个重定向
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'
此调用现在由 Laravel 网络应用而非 Wordpress 博客处理。那不是应该发生的事情。 这是由 nginx 配置引起的:
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;
}
为什么那部分配置会覆盖 proxy_pass?
nginx
根据各种规则评估 location
个块,如 explained by the documentation。
要使 location /blog/
的优先级高于 location ~ \.php$
,请使用 ^~
修饰符:
location ^~ /blog {
proxy_pass http://10.2.7.3;
proxy_set_header Host $host;
}