如何为websocket配置nginx。我在后端有REST的django。我在网上找到的标准配置不适用于nginx websocket

时间:2018-03-16 23:04:45

标签: django nginx websocket gunicorn nginx-reverse-proxy

我有nginx到服务器到浏览器。我希望nginx能够为来自浏览器的websocket请求服务。 Nginx使用代理配置在内部代理django(gunicorn uwsgi)。我无法在nginx中为websocket设置配置。从互联网上尝试了不同的配置,但没有成功。我在nginx配置文件中的默认文件:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /usr/share/gmc/dist;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404 /index.html index.js;
        }
        location ~ /redfish.* {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://localhost:8000;
        }
}

1 个答案:

答案 0 :(得分:0)

这些是您需要申请django项目的基本设置:

server {
 listen 80;
 server_name **Server Name or IP**;

 #Website LOGO:
 location = /favicon.ico { access_log off; log_not_found off;}

 #Static Files like CSS, JS
 location = /static/ {
       root /home/hitshell/website/project/
 }

 #Media Files like Images, Videos etc.
 location = /media/ {
       root /home/hitshell/website/project/
 }

 #proxy
 location = / {
        include proxy_params;
        proxy_pass http://unix:/home/hitshell/website/project/project.sock;
    }
 } 

<强>参考:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

https://www.shellvoide.com/hacks/installing-django-application-with-nginx-mysql-and-gunicorn-on-ubuntu-vps/

第一个在教程中间有一些缺少的配置。