linux - NGINX:允许多个端口可用于 https + 将所有 http 重定向到 https

标签 linux redirect nginx https

我正在尝试部署一个通过 https 托管两个 node.js Express 应用程序的 NGINX 服务器。

我的主站点(将在端口 80 上提供服务的站点)是一个在端口 8001 上运行的 Express 应用程序。(即 https://example.com 加载此应用程序)

我还在端口 8002 上运行另一个 Express 应用程序,我希望它在端口 8080 上公开可用。(即 https://example.com:8080 加载此应用程序)

这是我的 /etc/nginx/sites-available/default 文件:

server {
   listen 80;
   listen [::]:80;
   server_name example.com www.example.com;
   return 301 https://$server_name$request_uri;
}

server {
  # SSL configuration
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name example.com www.example.com;
  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;

  # Pass requests for / to localhost:8001:
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:8001/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
  }

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

server {
  listen 8080 ssl;
  server_name example.com www.example.com;
  return 301 https://$server_name$request_uri;

  include snippets/ssl-example.com.conf;
  include snippets/ssl-params.conf;

  # pass requests to port 8002 where our other node server is running
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:8002/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $http_host;
    proxy_cache_bypass $http_upgrade;
    proxy_redirect off;
  }
}

如果有任何额外帮助,我一直在按照 DigitalOcean 指南配置 https 和 NGINX herehere .

最佳答案

从第三个服务器 block 中删除 return 301 https://$server_name$request_uri;

关于linux - NGINX:允许多个端口可用于 https + 将所有 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732568/

相关文章:

linux - 如何在同一目录中复制文件名已更改的文件

linux - 帮助网站在 FF/Linux 上的布局

apache - 如果 URL 包含电子邮件地址,则 .htaccess 重定向

c - Libcurl 不会在重定向时发送 cookie

logging - 如何在 Nginx 日志中查看不可用的服务器?

nginx - "jrcs/letsencrypt-nginx-proxy-companion" docker 镜像 : too many certificates already issued for exact set of domains

c - 为什么这个基准代码使用如此高的 CPU?

python - 任务失败,因为返回代码为 -1,而预期为 0 - Bamboo

redirect - IE6 和 IE7 在重定向时发送没有主机名的 HTTP/1.1

Laravel 4 的 nginx 配置