我在 nginx 中的反向代理后面提供 WordPress 服务。我希望能够提供位于不同根目录中的静态页面。
我试过这个:
server {
listen 80;
listen [::]:80;
server_name ${NGINX_HOST};
root /static-pages;
sendfile on;
tcp_nodelay on;
location / {
try_files $uri $uri/ /static-pages/ =404;
#try_files $uri $uri/ =404;
#try_files /static-pages $uri/ =404;
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://wordpress;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
WordPress 运行良好,但是当我尝试访问其中一个静态页面时,我不断收到 404。当然,我可以location
向每个静态页面添加一个指令,但这似乎是一种开销。我可以让它发挥作用吗?
这是我认为相关的调试日志的一部分:
[debug] 7#7: *2 generic phase: 12
[debug] 7#7: *2 try files handler
[debug] 7#7: *2 http script var: "/marketing-analyst"
[debug] 7#7: *2 trying to use file: "/marketing-analyst" "/static-pages/marketing-analyst"
[debug] 7#7: *2 http script var: "/marketing-analyst"
[debug] 7#7: *2 trying to use dir: "/marketing-analyst" "/static-pages/marketing-analyst"
[debug] 7#7: *2 try file uri: "/marketing-analyst"
该目录当然/static-pages/marketing-analyst
存在。
答案1
工作解决方案:
server {
error_log /var/log/nginx/error.log debug;
listen 80;
server_name ${NGINX_HOST};
sendfile on;
tcp_nodelay on;
root /static-pages;
index index.html index.php;
try_files $uri $uri/ @proxy;
location @proxy {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://wordpress$uri;
proxy_set_header X-NginX-Proxy true;
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_set_header Host $http_host;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
}
location = / {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://wordpress;
proxy_set_header X-NginX-Proxy true;
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_set_header Host $http_host;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
}
}
如果我能挤压两个相似的代理,我会很高兴,但没什么大不了的。
答案2
差不多了。解决方案是首先尝试静态文件,然后转到反向代理(wordpress)作为后备:
server {
error_log /var/log/nginx/error.log debug;
listen 80;
listen [::]:80;
server_name ${NGINX_HOST};
sendfile on;
tcp_nodelay on;
root /static-pages;
location / {
try_files $uri $uri/ /static-pages/ @proxy;
}
location @proxy {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_pass http://wordpress;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
参考:
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#proxy-everything
但!当我尝试访问时,/
我收到 403 访问被拒绝/static-pages/index.html
(该信息不存在)。/index.html
如果没有与请求匹配的目录,我怎样才能告诉它不检查并返回到代理?