我的nginx部署了一个django项目还部署了一个vue前端,两个都配置了域名,可是两个域名都会访问到django的项目

server {
    listen      80;
    server_name www.****.com;
    charset     utf-8;
    access_log      /var/log/nginx/logindemo_access.log;
    error_log       /var/log/nginx/logindemo_error.log;
    client_max_body_size 75M;

    location /media  {
        alias /tmp/ssr/media;
    }

    location /static {
        alias /tmp/ssr/static;
    }

    location / {
        uwsgi_pass  127.0.0.1:8000;
        include     /etc/nginx/uwsgi_params;
    }
}
server {
        listen 8080;
        server_name www.***.club;
        root /tmp/dist;
        index index.html;
}

两个域名都是通过cloudflare解析的,然后在nginx部署,但是两个域名都会访问到一个项目
然后我将第二个配置的端口从8080改成80就可以通过域名访问第二个项目了,这是什么原因导致的

阅读 3.3k
2 个回答

前后端分离的项目,前端和后端可以用不同的域名,也可以用相同的域名
一、前端使用www.xxx.com,后端使用api.xxx.com

server {
    server_name  www.xxx.com;

    location / {
        root /tmp/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
server {
    server_name  api.xxx.com;
    location / {
        uwsgi_pass  127.0.0.1:8000;
        include     /etc/nginx/uwsgi_params;
    }
}

二、前端使用www.xxx.com,后端使用www.xxx.com/api/
1、uwsgi如果是使用http方式可以这样配

server {
    server_name  www.xxx.com;

    location / {
        root /tmp/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location ^~ /api/ {
        proxy_pass http://127.0.0.1:8000/;
    }
}

2、uwsgi如果是使用socket方式的话需要这样配

server {
    server_name  www.xxx.com;

    location / {
        root /tmp/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location ^~ /api/ {
        proxy_pass http://127.0.0.1:8080/;
    }
}
server {
    listen 8080;
    location / {
        uwsgi_pass  127.0.0.1:8000;
        include     /etc/nginx/uwsgi_params;
    }
}

通过http访问一个ip或者域名的时候如果没有指定端口,默认是80端口

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题