Nginx 将特定端口重写为子路径(nginx + uwsgi + django)

Nginx 将特定端口重写为子路径(nginx + uwsgi + django)

我的目标是重写下面的所有http://localhost:8000内容http://localhost/foo

我遵循了本文中提到的每一个步骤关联,但仍然无法让它工作。

这是我的设置:

urls.py(Django)

urlpatterns = [
    url(r'^$', 'home.view'),
    url(r'^foo$', 'foo.view'),
]

uwsgi.ini (uWSGI)

[uwsgi]

chdir           = /home/user/folder/project
wsgi-file       = /home/user/folder/project/iTrends/wsgi.py
home            = /home/user/.pyenv/versions/project
master          = true
processes       = 10
threads         = 5
socket          = /tmp/project.sock
chmod-socket    = 666
vacuum          = true
logto           = /tmp/project_uwsgi.log
http-websocket  = true
buffer-size     = 65535

nginx80.conf(带有端口 80 的 nginx)

server {
    listen       80;
    server_name = _;

    location /test {
        rewrite ^/test/(.*)$ /$1 break;   
        proxy_pass  http://127.0.0.1:8000;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

nginx8000.conf(nginx 端口为 8000)

server {
    listen      8000;
    server_name _; 
    charset     utf-8;

    client_max_body_size 75M;

    location /static {
        alias /home/user/folder/project/static_all;
    }

    location / {
        uwsgi_pass  unix:///tmp/project.sock;
        include     /etc/nginx/uwsgi_params;
    }
}

现在 http://localhost/test相当于http://localhost:8000

但是当我转到时http://localhost/test/foo,nginx 告诉我“未找到”
(我希望这与相同http://localhost:8000/foo

我错过了什么?

答案1

问题似乎出在重写规则中(第一个参数中)多余的 $。我删除了它,它现在开始正常工作了。

    location /test {
        rewrite ^/test/(.*) /$1 break;   
        proxy_pass  http://127.0.0.1:8000;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

相关内容