在 NGINX 中将 HTTP 重定向到 HTTPS,闪亮的服务器不工作

Redirect HTTP to HTTPS in NGINX with shiny server not working

我正在按照 this 教程设置闪亮的服务器并通过 nginx 将 http 重定向到 https。

我用下面的配置设置了我的 /etc/nginx/sites-available/example.com。我设法通过 https://example.com. This means nginx is correctly routing https://example.com to http://example.com:3838/myapp.

访问闪亮的应用 myapp

问题是当我访问 http://example.com nginx does not redirect me to https://example.com 时,它应该是这样。

知道为什么会这样吗?

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

server {
   listen 443 ssl;
   server_name example.com www.example.com;
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_prefer_server_ciphers on;
   ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

   location / {
       proxy_pass http://my_server_ip:3838/myapp;
       proxy_redirect http://my_server_ip:3838/myapp/ https://$host/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection $connection_upgrade;
       proxy_read_timeout 20d;
   }
}

我认为错误就在这里。一方面,您应该输入应用程序的地址,例如127.0.0.1,另一方面你想转发到文件夹/myapp/。但这与 Nginx 不同。

从 proxy_pass 和 proxy_redirect

中删除:/myapp

Link