Nginx位置重写代理后不起作用

我正在创build一个简单的nginx web服务器。 有几个PHP文件和一些静态页面,我已经将它们分成不同的文件夹,/ data / webjp中的php文件和/ data / webjp_static中的html文件。 这里是configuration文件:

server { listen 80; location / { proxy_pass http://127.0.0.1:7900/; proxy_store on; proxy_set_header Host $host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 7900; server_name 127.0.0.1; root /data/webjp/weber; index index.html index.php; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /data/webjp/weber/index.php; } location ~ /erarticles/ { root /data/webjp_static; } if (-f $request_filename) { break; } location ~* \.html$ { expires -1; } } 

看来“location〜/ erarticles /”块不起作用。 当我试图访问http://192.168.1.118/erarticles/56b1be02e33f6e3c6f000000/2016001.html时 ,我得到了404。

但是,如果我把代码服务器块中的“location〜/ erarticles /”块,它确实有效。 为什么?

在阅读理查德·史密斯的回答之后,我发现了一条线索。 实际上,我的项目是和Yii框架一起工作的,当我把它放在这里的时候,我省略了几行configuration文件。

 if (!-f $request_filename) { rewrite ^/(.+)$ /index.php?url=$1 last; break; } 

这个问题与反向代理无关。 7900的服务器块有多个问题。

默认的文档根目录被设置为PHP目录,所以location ~* \.html$块永远不能工作。

我不知道if (-f $request_filename) { break; } if (-f $request_filename) { break; }应该这样做。

location ~ .*\.(php|php5)?$块匹配URIs . 。 你声明你有一些.php文件,但你只返回index.php

你有没有考虑过使用try_files ? 例如(这只是一个起点):

 server { listen 7900; root /data/webjp_static; index index.html index.php; location / { try_files $uri $uri/ =404; } location ~ \.php5?$ { root /data/webjp/weber; try_files $uri =404; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } location ~* \.html$ { expires -1; } } 

请注意, index指令不会find任何index.php文件,因为它们将它们保存在与.html文件不同的目录中。

先阅读这个文件 。