简体   繁体   English

Nginx自动将HTTP重定向到HTTPS

[英]Nginx Redirect HTTP to HTTPS automatically

I am trying to redirect all my traffic from http to https automatically. 我正在尝试将所有流量从http自动重定向到https How can i do a 301 redirect to all my domain and subdomains? 301如何重定向到我的所有域和子域?

This is NGNIX Config file 这是NGNIX的配置文件

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;
    }
}

The default server will accept http connections for any server name (without an explicit server block). 默认服务器将接受任何服务器名称的http连接(没有显式的server块)。 Use the $host variable to determine the name of the requested domain. 使用$host变量确定所请求域的名称。

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

See this document for more. 有关更多信息,请参见此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM