首页 > 解决方案 > 使用 NGinx 路由网站(PHP 和 Django)

问题描述

我的第一个网站在显示主页的 Apache Web 服务器下正常运行,可以通过http://localhost:8989/访问它。

我还有我的第二个网站,它是一个 Django 应用程序(在 uWsgi 下运行)在http://localhost:8000/上正常运行,并且每个页面都正确显示。

现在,我想使用 Nginx 来服务两个网站的所有静态资源。由于我想为这两个网站使用我唯一的一个域,即 www.test.com (TEST),我将其配置如下:

   server {
        listen 80;        
        server_name _;

        location / {
                root /var/www/statics;
                index index.html index.htm;
                try_files $uri @home;
        }

        #for request: https://www.test.com/ , it should go to the 1st website
        location @home {
                proxy_pass http://localhost:8989;
                proxy_set_header Host $host;
                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;
        }

        #for request: https://www.test.com/abc , it should go to the 2st website
        location /abc {
                rewrite ^/abc(.*) $1 break;

                try_files $uri @abc;
        }

        location @abc {
                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;
                proxy_set_header X-Forwarded-Proto $scheme;
        }
   }

不知道有没有更好的配置,我的要求如下:

上述配置的当前状态:

有没有人有过这样的经历?在 Nginx 配置中应该执行什么正确的正则表达式或重写?还是应该在 Django 配置上完成?我不想更改 Django 项目的每个应用程序的 urls.py,但如果实际上需要对其 settings.py 文件执行一个简单的技巧,那就太好了。请帮忙

标签: djangoapachenginx

解决方案


推荐阅读