【问题标题】:Nginx Redirect HTTP to HTTPS automaticallyNginx 自动将 HTTP 重定向到 HTTPS
【发布时间】:2021-07-09 05:36:52
【问题描述】:

我正在尝试自动将我的所有流量从http 重定向到https。如何对我的所有域和子域进行 301 重定向?

这是 NGINX 配置文件

upstream app_server {
    server unix:/run/DigitalOceanOneClick/unicorn.sock fail_timeout=0;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name sprintsocial.io app.sprintsocial.io admin.sprintsocial.io;
#       return 301 https://$server_name$request_uri;
}

server {
    #listen   80;
    listen 443;
    root /home/rails/sprintsocial/public;
    #server_name _;
    server_name sprintsocial.io app.sprintsocial.io admin.sprintsocial.io;
    ssl on;
    ssl_certificate /home/sprintsocial.io.chained.crt;
    ssl_certificate_key /home/sprintsocial.io.key;
    index index.htm index.html;
#    return 301 https://$server_name$request_uri;

#    rewrite ^/(.*) https://app.sprintsocial.io/$1 permanent;
#    rewrite ^/(.*) https://admin.sprintsocial.io/$1 permanent;

    location / {
            try_files $uri/index.html $uri.html $uri @app;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                    try_files $uri @app;
            }

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
    }
}

【问题讨论】:

标签: ruby-on-rails redirect nginx https


【解决方案1】:

默认服务器将接受任何服务器名称的http 连接(没有明确的server 块)。使用$host 变量来确定请求的域的名称。

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    return 301 https://$host$request_uri;
}

请参阅this document 了解更多信息。

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2018-08-18
    • 2017-02-25
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多