Nginx重写某些端口到子path(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(带端口8000的nginx)

 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

我错过了什么?

看来问题是重写规则中的额外$(在第一个参数中)。 我删除它,它已经开始工作正常。

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