大网数据广告活动
服务器运维问答

nginx not redirecting to https

· 52CE 我爱测

I am following the instructions on this page to setup an R Shiny server on a DigitalOcean droplet. What I would like to have: A Shiny server running on shiny.domain.com Automatic redirect from shiny.domain.com to https://shiny.do…

问题原文

原帖链接 · nico · CC BY-SA 4.0

I am following the instructions on this page to setup an R Shiny server on a DigitalOcean droplet.

What I would like to have:

  1. A Shiny server running on shiny.domain.com
  2. Automatic redirect from shiny.domain.com to https://shiny.domain.com
  3. Automatic redirect from http://shiny.domain.com to https://shiny.domain.com

At the moment 1 and 2 work, but 3 does not. If I visit https first then http gets redirected to https, but if I use http the first time (e.g. in an incognito window) I get Nginx welcome page instead.

My Nginx configuration is as follows (Shiny server listens to 3838, so a reverse proxy is setup to automatically redirect traffic, so I don't have to type :3838 every time, as explained in the link above)

server {
listen 80;
listen [::]:80;

# redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate <path to certificate>;
    ssl_certificate_key <path to key>;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    ssl_dhparam /etc/nginx/snippets/dhparam.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers xxxxxx
    ssl_prefer_server_ciphers off;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # OCSP stapling
    ssl_stapling on;

    ssl_stapling_verify on;

    ssl_trusted_certificate <path to chain.pem>

    server_name shiny.domain.com;  

    location / {
        proxy_pass http://localhost:3838;
        proxy_redirect http://localhost:3838/ $scheme://$host/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_read_timeout 20d;
        proxy_buffering off;
        }
    }

I am extremely new to nginx, so I would appreciate some help with this

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

Your server block for http is missing server_name. This means nginx will use the default_server for these requests, which will display the welcome page.

Replace your first block with:

server {
    listen 80;
    listen [::]:80;

    server_name shiny.example.com;

    return 301 https://shiny.example.com;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-12-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。