Nginx 如何配置非 80 端口

Hubery_Jun
  • 4
新手上路,请多包涵

部署环境:Django + docker + Gunicorn + Daphne + nginx

问题:现在是项目已成功部署到阿里云服务器上,也添加了域名解析,可用通过 域名:8111 访问项目。

我该怎么配置才能不加端口,只要用域名就能访问项目呢?以下是我配置的 nginx:

upstream hello_server {
      server djangoapp:8111;
    }


upstream channels-backend {
      server djangoapp:8181;
}

server {
    listen 80;
    server_name localhost;

    client_max_body_size 30M; 
    client_body_buffer_size 5000K;

    location / {
        proxy_pass http://hello_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        
    }

    location /static/ {
        alias /opt/services/djangoapp/src/CBS/static_all/;
    }

    # django-channels 
    location /ws/ {
        proxy_pass http://channels-backend;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        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-Host $server_name;
    }

}
回复
阅读 2.5k
5 个回答
...
listen 80;
server_name localhost;
...

完成域名绑定,你把 localhost 改为你的域名应该就可以了

Hubery_Jun
  • 4
新手上路,请多包涵

改 localhost 不行呢,还是不能解决

不带端口就是默认访问80端口,80端口找服务器商备案后开通

做一层端口转发即可

估计你想问反向代理
每个域名需要一个server

server {
    listen 80;
    server_name www.channelbackend.com; // 域名自己换
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://channels-backend;
    }
}

访问 www.channelbackend.com就会显示channels-backend内容

宣传栏