通过nginx将http重定向到https

时间:2016-08-19 03:59:08

标签: nginx https

我在Ubuntu 16.04上有一个完全库存的nginx版本:nginx / 1.10.0(Ubuntu),而不是/ etc / nginx / sites-enabled / default被替换为以下...

server {
    listen 443 default_server;
    server_name www.example.com;

    ssl_certificate           /etc/nginx/cert.crt;
    ssl_certificate_key       /etc/nginx/cert.key;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://localhost:8080; # my existing apache instance
        proxy_set_header Host $host;

        # re-write redirects to http as to https, example: /home
        proxy_redirect http:// https://;
    }
}

其目的是成为围绕在端口8080上运行的http api的https包装器。

我现在正试图让它成为当有人在http端口80上访问我的网站时,它会将它们重定向到https。我已经尝试了所有其他文档修复,主要是基于这样的......

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

......似乎没有任何影响,所以你总是得到WELCOME TO NGINX页面。

3 个答案:

答案 0 :(得分:1)

来自this问题,

您缺少配置中的server_name参数。

server {
       listen         80;
       server_name    my.domain.com;
       return         301 https://$server_name$request_uri;
}

答案 1 :(得分:0)

原来我的工作有一个非常激进的缓存代理,无论我做了什么只是返回默认的nginx页面。

用..修复

    // ...
    location / {
        proxy_pass http://localhost:8080; # my existing apache instance
        proxy_set_header Host $host;

        # re-write redirects to http as to https, example: /home
        proxy_redirect http:// https://;
        add_header Cache-Control "no-cache";
    }
}

答案 2 :(得分:0)

我想在此处留下更新的更新,因为自问这个问题后nginx发生了变化。

您可以使用以下行重定向http请求:

server {
    listen      80;
    server_name example.com;
    rewrite     ^   https://example.com$request_uri? permanent;
}