首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Nginx上反向代理,将所有http请求重定向到具有特定端口号的https。

在Nginx上反向代理,将所有http请求重定向到具有特定端口号的https。
EN

Stack Overflow用户
提问于 2022-12-03 19:36:57
回答 1查看 22关注 0票数 0

我有两个nodejs应用程序运行在同一个具有不同端口的NGINX服务器上。例如:

frontend.js on端口3000可在上访问

backend.js on 3001可在上访问

甚至一个网站也是在同一台服务器上运行的:)使用相同域的,可以使用

:80) (默认端口即

)

现在,我想确保所有这3 url 只接受https。如果收到对非https(即http)的任何请求,则应该将其重定向到各自的"https url"

我已经尝试了多种解决方案提供的互联网,包括堆栈溢出,通过一些调整,但尚未成功。

贝洛斯是我尝试过的解决方案。

--这两种尝试过的解决方案--都没有起作用,包括几十个其他的解决方案。很可能是因为我不能很好地抓住它,因为我对Nginx.的反向Pxoxy设置完全陌生。

以下是我尝试过的解决方案的结果。

http://example.com 重定向到 https版本

http://example.com:3000 不重定向到 https版本&如果尝试直接打开https版本会显示错误(SSL_ERROR_RX_RECORD_TOO_LONG)

http://example.com:3001 不重定向到 https版本&如果尝试直接打开https版本会显示错误(SSL_ERROR_RX_RECORD_TOO_LONG)

完全错误显示在Firefox浏览器中:

安全连接失败连接到byteglance.com:3003时发生错误。SSL收到的记录超过了最大允许长度。错误代码: SSL_ERROR_RX_RECORD_TOO_LONG

解决方案1:尝试将所有流量重定向到https

代码语言:javascript
复制
    server
    {
        listen 80;
        #listen [::]:80;
        server_name example.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/example.com;

        #include rewrite/none.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        location / {
            return 301 https://$host$request_uri;
        }

        access_log off;
    }

    server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name example.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/example.com;

        ssl_certificate /usr/local/nginx/conf/ssl/example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/example.com/example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-.....CHACHA20-RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/none.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
    }

解决方案2:尝试使用Nginx反向代理来实现解决方案

代码语言:javascript
复制
    server
    {
        listen 80;
        #listen [::]:80;
        server_name example.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/example.com;

        #include rewrite/none.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        location / {
            return 301 https://$host$request_uri;
        }

        access_log off;
    }

    server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name example.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/example.com;

        ssl_certificate /usr/local/nginx/conf/ssl/example.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/example.com/example.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-.....CHACHA20-RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include rewrite/none.conf;
        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;

        location ~* \.(js)$
         {
          proxy_pass http://localhost:3000
          proxy_http_version 1.1
          proxy_cache_bypass $http_upgrade
          proxy_set_header Upgrade $http_upgrade
          proxy_set_header Connection “upgrade”
          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
          proxy_set_header X-Forwarded-Host $host
          proxy_set_header X-Forwarded-Port $server_port
         }


        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log off;
    }

有人能看一下我的配置并纠正我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2022-12-03 20:30:16

听起来,SSL证书没有为端口30003001上运行的域正确设置。为了在特定端口上为HTTPS通信提供服务,您需要确保SSL证书涵盖该域和端口。例如,如果您的SSL证书仅覆盖域example.com,则它对域example.com:3000example.com:3001无效。

解决此问题的一种方法是生成一个新的SSL证书,该证书涵盖您希望为HTTPS流量提供服务的所有域和端口。您可以使用像“让我们加密”这样的服务来生成覆盖多个域的免费SSL证书。

一旦您拥有了新的SSL证书,您将需要更新NGINX配置以使用新证书。在侦听端口443 ( HTTPS端口)的server块中,需要更新ssl_certificatessl_certificate_key指令,以指向新的SSL证书及其私钥。

在更新了NGINX配置并重新加载服务器之后,SSL证书覆盖的域上的所有HTTPS通信都应该得到正确的服务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74670049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档