Nginx配置同一個域名同時支持http與https兩種方式訪問實(shí)現(xiàn)
Nginx配置同一個域名http與https兩種方式都可訪問,證書是阿里云上免費(fèi)申請的
server { listen 80; listen 443 ssl; ssl on; server_name 域名; index index.html index.htm index.php default.html default.htm default.php; ssl_certificate /usr/local/nginx/cert/21402058063066221.pem; //下載申請后阿里ssh提供的pem ssl_certificate_key /usr/local/nginx/cert/21402058063066221.key;//下載申請后阿里ssh提供的key ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; root /home/wwwroot/網(wǎng)站目錄; include laravel.conf; //好吧,這里是laravel配置,不一定合適您哈,請或略 #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/airclass.mime.org.cn.log; }
關(guān)鍵在于上面的listen 80;
listen 443 ssl; 開啟80端口
當(dāng)然,這樣玩就沒有啥意義了,既然是https,就完全沒必要http傳輸數(shù)據(jù)啦.我們必須把所有http請求轉(zhuǎn)發(fā)到https,
把http重定向到https使用了nginx的重定向命令。那么應(yīng)該如何寫重定向?之前老版本的nginx可能使用了以下類似的格式。
也就是再添加一個虛擬機(jī)server,80端口一個
server { listen 80; server_name www.domain.com; rewrite ^/(.*) https://$server_name$1 permanent; #跳轉(zhuǎn)到Https }
重寫依舊不同版本可能如下
rewrite ^/(.*)$ https://domain.com/$1 permanent;
或者
rewrite ^ https://domain.com$request_uri? permanent;
現(xiàn)在nginx新版本已經(jīng)換了種寫法,上面這些已經(jīng)不再推薦?,F(xiàn)在網(wǎng)上可能還有很多文章寫的是第一種。
下面是nginx http頁面重定向到https頁面最新支持的寫法:
server { listen 80; server_name domain.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name domain.com; }
但是我的nginx/1.10.0好像跑不起來,也許不支持這種寫法吧...
下面是基于http轉(zhuǎn)https的完整配置:
server { #listen 80; listen 443; ssl on; server_name domain.com; //你的域名 index index.html index.htm index.php default.html default.htm default.php; ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem; ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; root /home/wwwroot/web/public;//項(xiàng)目根目錄 include laravel.conf; #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } } server { listen 80; server_name domain.com; rewrite ^/(.*) https://$server_name$request_uri? permanent; }
到此這篇關(guān)于Nginx配置同一個域名同時支持http與https兩種方式訪問實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx同域名同時支持http與https內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。