共计 2864 个字符,预计需要花费 8 分钟才能阅读完成。
使用NGINX做反向代理WordPress碰到后台没办法登录,经过一番尝试,记录一下设置,下面是宝塔的设置,其他的可以自行添加

#PROXY-START/
# 1. 精确匹配/wp-login.php路径回源
location ^~ /wp-login.php {
proxy_pass https://127.0.0.1:8080; # 后端WordPress地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; # 传递HTTPS协议
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
}
# 2. 匹配/wp-admin前缀(包括/wp-admin/下所有子路径)回源
location ^~ /wp-admin {
proxy_pass https://127.0.0.1:8080; # 后端WordPress地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
# 若/wp-admin下有AJAX请求(如/wp-admin/admin-ajax.php),确保不超时
proxy_send_timeout 180s;
}
# 其他路径
location ^~ /
{
proxy_pass https://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
expires 60m;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 600m;
}
#PROXY-END/
完整配置文件
server {
listen 443 ssl http2;
server_name your-domain.com;
# SSL配置(省略,保持原有)
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
# 1. 精确匹配/wp-login.php路径回源
location = /wp-login.php {
proxy_pass http://127.0.0.1:8080; # 后端WordPress地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; # 传递HTTPS协议
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
}
# 2. 匹配/wp-admin前缀(包括/wp-admin/下所有子路径)回源
location ^~ /wp-admin {
proxy_pass http://127.0.0.1:8080; # 后端WordPress地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
# 若/wp-admin下有AJAX请求(如/wp-admin/admin-ajax.php),确保不超时
proxy_send_timeout 60s;
}
# 其他路径配置(如静态资源CDN/其他代理,保持原有)
location ^~ /
{
proxy_pass https://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
expires 60m;
}
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 301 302 600m;
}
}
正文完