使用 Nginx 和 Daphne 升级 websocket。仅当我在地址栏中输入端口号时才有效

使用 Nginx 和 Daphne 升级 websocket。仅当我在地址栏中输入端口号时才有效

首先,我不太熟悉服务器代理,但是我可以稍微了解一下。

场景如下。

我正在 docker 容器中运行一个应用程序,在 apache 服务器上使用 django、nginx、daphne、redis。

  1. 使用 example.com 访问由 apache 运行的网站。
  2. Apache 将代理传递给 Nginx(在容器中运行)80:8080 和 443:8443
  3. 然后 Nginx 传递给 Daphne 来运行容器中端口 8000 上运行的实际应用程序

该网站运行完美,除了访问 example.com 时 websocket 连接返回 404 但是如果我访问 example.com:8443,websocket 就会按预期工作。

我尝试将端口号与代理传递一起传递,但它似乎从未出现在请求标头中。

我只是真的需要知道去哪里查找才能解决这个问题。这是 Apache、Nginx 还是 Daphne 的问题?

我尝试了很多方法,目前不值得一一列举。我希望当我在地址栏中输入端口号时,websockets 可以正常工作,这是一个线索。

预先感谢您的帮助!

更新

传递给 Nginx 的 Apache 指令

    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    ProxyPass        "/" "https://localhost:8443/"
    ProxyPassReverse "/" "https://localhost:8443/"

Nginx 配置

upstream app {
    server app:8000;
}

server {
    listen 80;
    server_name secret.com;

    location / {
        proxy_pass http://app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $http_host;
    }

    location /static/ {
        alias /static/;
    }
}

server {
    listen 443 ssl;
    server_name secret.com;
    ssl_certificate /etc/nginx/certs/srv1.videsignz.com.crt;
    ssl_certificate_key /etc/nginx/private/srv1.videsignz.com.key;

    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_pass http://app;

        proxy_ssl_certificate /etc/nginx/certs/srv1.videsignz.com.crt;
        proxy_ssl_certificate_key /etc/nginx/private/srv1.videsignz.com.key;

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

        proxy_set_header Host $http_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;
        proxy_set_header X-Forwarded-Port $server_port;
    }

    location /static/ {
        alias /static/;
    }
}

Docker Compose 文件

version: '3.7'

services:
  app:
    volumes:
      - assets:/assets
    image: videsignz/ktech:latest
    ports:
      - "8000:8000"
  nginx:
    build: ./nginx
    volumes:
      - assets:/assets
      - /etc/ssl/certs:/etc/nginx/certs
      - /etc/ssl/private:/etc/nginx/private
    ports:
      - "8080:80"
      - "8443:443"
    depends_on:
      - app
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    depends_on:
      - app

volumes:
  assets:

入口点文件

#!/bin/sh

python manage.py collectstatic --no-input

daphne -b 0.0.0.0 -p 8000 core.asgi:application

完整的 Apache 虚拟主机配置端口 80 和端口 443

<VirtualHost 198.46.134.221:80>
  ServerName connect.ktechonline.com
    ServerAlias mail.connect.ktechonline.com www.connect.ktechonline.com
  DocumentRoot /home/connktechonline/public_html
  ServerAdmin [email protected]
  UseCanonicalName Off

  ## User connktechonline # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir disabled
          UserDir enabled connktechonline 
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule include_module>
    <Directory "/home/connktechonline/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>

  

  <IfModule suphp_module>
    suPHP_UserGroup connktechonline connktechonline
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup connktechonline connktechonline
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid connktechonline connktechonline
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID connktechonline connktechonline
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser connktechonline
    PassengerGroup connktechonline
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/connktechonline/public_html/cgi-bin/
  </IfModule>


    # Global DCV Rewrite Exclude
    <IfModule rewrite_module>
        RewriteOptions Inherit
    </IfModule>

        
        
            
                
                
  Include "/etc/apache2/conf.d/userdata/std/2_4/connktechonline/connect.ktechonline.com/*.conf"


  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/std/2_4/connktechonline/connect.ktechonline.com/*.conf"
</VirtualHost>
<VirtualHost 198.46.134.221:443>
  ServerName connect.ktechonline.com
  ServerAlias mail.connect.ktechonline.com www.connect.ktechonline.com webmail.connect.ktechonline.com cpcontacts.connect.ktechonline.com autodiscover.connect.ktechonline.com cpcalendars.connect.ktechonline.com cpanel.connect.ktechonline.com webdisk.connect.ktechonline.com
  DocumentRoot /home/connktechonline/public_html
  ServerAdmin [email protected]
  UseCanonicalName Off

  ## User connktechonline # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir disabled
          UserDir enabled connktechonline 
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule mod_include.c>
    <Directory "/home/connktechonline/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>

  
  <Proxymatch ^https?://127\.0\.0\.1:(2082|2083|2077|2078|2079|2080|2086|2087|2095|2096)/>
       <IfModule security2_module>
          SecRuleEngine Off
       </IfModule>
  </Proxymatch>

  <IfModule mod_suphp.c>
    suPHP_UserGroup connktechonline connktechonline
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup connktechonline connktechonline
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid connktechonline connktechonline
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID connktechonline connktechonline
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser connktechonline
    PassengerGroup connktechonline
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/connktechonline/public_html/cgi-bin/
  </IfModule>
  <IfModule ssl_module>
    SSLEngine on
    
    SSLCertificateFile /var/cpanel/ssl/apache_tls/connect.ktechonline.com/combined

    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
    <Directory "/home/connktechonline/public_html/cgi-bin">
      SSLOptions +StdEnvVars
    </Directory>
  </IfModule>
        
        
            
                
    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    ProxyPass        "/" "https://localhost:8443/"
    ProxyPassReverse "/" "https://localhost:8443/"




  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/ssl/2_4/connktechonline/connect.ktechonline.com/*.conf"

    <IfModule headers_module>
    RequestHeader set X-HTTPS 1
    </IfModule>
    RewriteEngine On
                RewriteCond %{HTTP_HOST} =autodiscover.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =autodiscover.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^ http://127.0.0.1/cgi-sys/autodiscover.cgi [P]
                RewriteCond %{HTTP_HOST} =cpanel.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =cpanel.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^/(.*) /___proxy_subdomain_cpanel/$1 [PT]
        ProxyPass "/___proxy_subdomain_cpanel" "http://127.0.0.1:2082" max=1 retry=0
                RewriteCond %{HTTP_HOST} =cpcalendars.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =cpcalendars.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^/(.*) /___proxy_subdomain_cpcalendars/$1 [PT]
        ProxyPass "/___proxy_subdomain_cpcalendars" "http://127.0.0.1:2079" max=1 retry=0
                RewriteCond %{HTTP_HOST} =cpcontacts.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =cpcontacts.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^/(.*) /___proxy_subdomain_cpcontacts/$1 [PT]
        ProxyPass "/___proxy_subdomain_cpcontacts" "http://127.0.0.1:2079" max=1 retry=0
                RewriteCond %{HTTP_HOST} =webdisk.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =webdisk.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^/(.*) /___proxy_subdomain_webdisk/$1 [PT]
        ProxyPass "/___proxy_subdomain_webdisk" "http://127.0.0.1:2077" max=1 retry=0
                RewriteCond %{HTTP_HOST} =webmail.connect.ktechonline.com [OR]
                RewriteCond %{HTTP_HOST} =webmail.connect.ktechonline.com:443
            RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

            RewriteRule ^/(.*) /___proxy_subdomain_webmail/$1 [PT]
        ProxyPass "/___proxy_subdomain_webmail" "http://127.0.0.1:2095" max=1 retry=0

                RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                    RewriteCond %{HTTP_HOST} =cpanel.connect.ktechonline.com [OR]
                    RewriteCond %{HTTP_HOST} =cpanel.connect.ktechonline.com:443

                RewriteRule ^/(.*) /___proxy_subdomain_ws_cpanel/$1 [PT]
                RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                    RewriteCond %{HTTP_HOST} =webmail.connect.ktechonline.com [OR]
                    RewriteCond %{HTTP_HOST} =webmail.connect.ktechonline.com:443

                RewriteRule ^/(.*) /___proxy_subdomain_ws_webmail/$1 [PT]

    RewriteRule ^/Microsoft-Server-ActiveSync /___proxy_activesync/$1 [PT]
    ProxyPass "/___proxy_activesync" "http://127.0.0.1:2090/Microsoft-Server-ActiveSync" max=1 retry=0
</VirtualHost>

相关内容