【问题标题】:How to configure nginx from http to https如何配置 nginx 从 http 到 https
【发布时间】:2017-03-23 11:30:57
【问题描述】:

我有一个 Ubuntu Node.js 服务器与我的 http://www.example.com 网站一起工作。 我使用 httpx://localhost:3000 进行测试,然后当我将其部署到 Ubuntu 时, 我仍然必须输入端口(www.example.com:3000)。有人告诉我实施 反向代理以删除端口 3000 要求。我安装了 nginx 并添加了 下列的: sudo nano /etc/nginx/sites-available/default ----------删除所有然后复制/粘贴--------------

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://67.205.128.21:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

这行得通,并且取消了输入端口 3000 的要求。 然后我发现我需要使用 SSL/证书运行我的应用程序。 我能够对 nginx 进行更改以使其以https://www.example.com:3000 工作。 但现在我需要摆脱端口 3000 的要求。 我尝试了与 http: 相同的反向代理设置,但没有奏效。 如何配置 nginx 以删除端口 3000 要求。 以下是我在浏览器中输入时当前发生的情况:

http://67.205.128.21    - Works
http://example.com  - Redirects to  https://example ; Error: Redirects too many times
http://www.example.com  - Redirects to  https://example ; Error: Redirects too many times
http://example.com:3000 - Works
http://www.example.com:3000 - Works

当前 nginx 配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
    location ~ /.well-known {
            allow all;
    }
    # SSL configuration
    #
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
server_name example.com;
    location / {
    proxy_pass http://67.205.128.21:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
}

【问题讨论】:

    标签: node.js ubuntu nginx


    【解决方案1】:

    这应该可行:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        server_name example.com www.example.com;
    
        return 301 https://$server_name$request_uri;
    
        location ~ /.well-known {
            allow all;
        }
    }
    

    然后,在同一个文件或不同的文件中,添加一个额外的服务器块。

    # SSL configuration
    #
    server {
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
    
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;
    
        server_name example.com;
    
        location / {
            proxy_pass http://67.205.128.21:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    

    我认为问题在于您只有一个 server 块,因此当执行重定向时,它落在同一个服务器块中,然后再次重定向。

    【讨论】:

    • 我现在得到一个“502 Bad Gateway”。我对 nginx 一无所知,只是尝试按照参考资料中的说明进行操作。我认为我们正在接近解决方案。我该如何解决这个 502?谢谢,帕特
    • 好的,这就是进步。您的 Node.js 应用程序是否在端口 3000 上提供服务?检查它是否正在运行。很可能不是。查看您的 NGINX 错误日志文件(通常位于 /var/log/nginx/error.log
    • 2016/11/10 12:41:30 [错误] 7449#7449: *42 上游过早关闭连接,同时从上游读取响应标头,客户端:162.200.129.120,服务器:mysite10.com,请求:“GET / HTTP/1.1”,上游:“67.205.128.21:3000”,主机:“mysite10.com”2016/11/10 12:41:30 [错误] 7449#7449: *42 上游在读取时过早关闭连接来自上游的响应标头,客户端:162.200.129.120,服务器:mysite10.com,请求:“GET /favicon.ico HTTP/1.1”,上游:“67.205.128.21:3000/favicon.ico”,主机:“mysite10.com”,推荐人:“@ 987654323@"
    • 是的,Node App 在端口 3000 上提供服务
    • 我能够解决这个问题。更改:proxy_pass 67.205.128.21:3000 为 proxy_pass 67.205.128.21:3000。我需要重定向“https”而不是“http”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多