大网数据广告活动
服务器运维问答

Nginx location rewrite behind a proxy is not working

· 52CE 我爱测

I am creating a simple nginx web server. There are a few php files and some static pages, and I have separated them into different folders, the php files in /data/webjp and the html files in /data/webjp_static . Here is the confi…

问题原文

原帖链接 · wershest · CC BY-SA 3.0

I am creating a simple nginx web server. There are a few php files and some static pages, and I have separated them into different folders, the php files in /data/webjp and the html files in /data/webjp_static . Here is the configuration file:

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;
    }
}

It seems that the "location ~ /erarticles/" block is not working. When I tried to access http://192.168.1.118/erarticles/56b1be02e33f6e3c6f000000/2016001.html, I got 404.

But if I put the "location ~ /erarticles/" block in the proxy server block, it did works. Why?

After read the answer from Richard Smith, I found a clue. In fact, my project is working with Yii framework, and I omitted a few lines of the configuration file when I posted it here.

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

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The problem has nothing to do with the reverse proxy. The server block for 7900 has multiple issues.

The default document root is set to the PHP directory, so that the location ~* \.html$ block can never work.

I do not know what if (-f $request_filename) { break; } is supposed to do.

And the location ~ .*\.(php|php5)?$ block matches URIs like .. You state that you have some .php files, but you only return index.php.

Have you considered using try_files? For example (and this is only a starting point):

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;
    }
}

Note that the index directive will not find any index.php files as you keep them in a separate directory from the .html files.

Read this document first.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。