簡體   English   中英

通過nginx將http重定向到https

[英]Redirect http to https via nginx

我已經完整安裝了nginx版本:在/ etc / nginx / sites-enabled / default以外的Ubuntu 16.04上,nginx / 1.10.0(Ubuntu)已替換為以下內容...

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

...似乎沒有任何影響,您總是可以看到“歡迎來到NGINX”頁面。

根據這個問題,

您在配置中缺少server_name參數。

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

事實證明,我的工作有一個非常激進的緩存代理,無論我剛剛返回默認的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";
    }
}

由於要詢問此問題以來nginx發生了變化,因此我想在此保留最新的更新。

您可以使用以下幾行來重定向http請求:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM