Nginx強(qiáng)制跳轉(zhuǎn)Https(Http訪問(wèn)跳轉(zhuǎn)Https)
Nginx安裝注意事項(xiàng)
最近公司要上線HTTPS,需要把之前的HTTP訪問(wèn)強(qiáng)制跳轉(zhuǎn)到HTTPS
安裝的時(shí)候需要注意加上 --with-http_ssl_module,因?yàn)閔ttp_ssl_module不屬于Nginx的基本模塊。
#1.配置 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #2.編譯安裝 make && make install
配置SSL證書(shū)
如下兩個(gè)證書(shū)文件
ssl.crt
ssl.key
配置存放路徑為/usr/local/nginx/cert/
server {
listen 443;
server_name dev.wangsl.com;
root /var/www/XXX/public;
ssl on;
ssl_certificate /usr/local/nginx/cert/ssl.crt;
ssl_certificate_key /usr/local/nginx/cert/ssl.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5; //或者是ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
access_log /var/www/vhosts/www.wangsl.com/logs/clickstream_ssl.log main;
error_log /var/www/vhosts/www.wangsl.com/logs/clickstream_error_ssl.log;
if ($remote_addr !~ ^(124.165.97.144|133.110.186.128|133.110.186.88)) { //對(duì)訪問(wèn)的來(lái)源ip做白名單限制
rewrite ^.*$ /maintence.php last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 300;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
}
}
Http訪問(wèn)強(qiáng)制跳轉(zhuǎn)到Https的幾種方式:
一、采用nginx的rewrite方法
1) 下面是將所有的http請(qǐng)求通過(guò)rewrite重寫(xiě)到https上。
例如將所有的dev.wangsl.com域名的http訪問(wèn)強(qiáng)制跳轉(zhuǎn)到https。
下面配置均可以實(shí)現(xiàn):
#配置1:
server {
listen 80;
server_name dev.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
rewrite ^(.*)$ https://$host$1 permanent; //這是ngixn早前的寫(xiě)法,現(xiàn)在還可以使用。
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
================================================================
上面的跳轉(zhuǎn)配置rewrite ^(.*)$ https://$host$1 permanent;
也可以改為下面
rewrite ^/(.*)$ http://dev.wangsl.com/$1 permanent;
或者
rewrite ^ http://dev.wangsl.com$request_uri? permanent;
================================================================
#配置2:
server {
listen 80;
server_name dev.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
return 301 https://$server_name$request_uri; //這是nginx最新支持的寫(xiě)法
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
#配置3:這種方式適用于多域名的時(shí)候,即訪問(wèn)wangsl.com的http也會(huì)強(qiáng)制跳轉(zhuǎn)到https://dev.wangsl.com上面
server {
listen 80;
server_name dev.wangsl.com wangsl.com *.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
if ($host ~* "^wangsl.com$") {
rewrite ^/(.*)$ https://dev.wangsl.com/ permanent;
}
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
#配置4:下面是最簡(jiǎn)單的一種配置
server {
listen 80;
server_name dev.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
if ($host = "dev.wangsl.com") {
rewrite ^/(.*)$ http://dev.wangsl.com permanent;
}
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}二、采用nginx的497狀態(tài)碼
497 - normal request was sent to HTTPS
#解釋?zhuān)寒?dāng)網(wǎng)站只允許https訪問(wèn)時(shí),當(dāng)用http訪問(wèn)時(shí)nginx會(huì)報(bào)出497錯(cuò)誤碼
#配置實(shí)例:
#如下訪問(wèn)dev.wangsl.com或者wangsl.com的http都會(huì)被強(qiáng)制跳轉(zhuǎn)到https
server {
listen 80;
server_name dev.wangsl.com wangsl.com *.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
error_page 497 https://$host$uri?$args;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
#也可以將80和443的配置放在一起:
server {
listen 127.0.0.1:443; #ssl端口
listen 127.0.0.1:80; #用戶習(xí)慣用http訪問(wèn),加上80,后面通過(guò)497狀態(tài)碼讓它自動(dòng)跳到443端口
server_name dev.wangsl.com;
#為一個(gè)server{......}開(kāi)啟ssl支持
ssl on;
#指定PEM格式的證書(shū)文件
ssl_certificate /etc/nginx/wangsl.pem;
#指定PEM格式的私鑰文件
ssl_certificate_key /etc/nginx/wangsl.key;
#讓http請(qǐng)求重定向到https請(qǐng)求
error_page 497 https://$host$uri?$args;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}三、利用meta的刷新作用將http跳轉(zhuǎn)到https
上述的方法均會(huì)耗費(fèi)服務(wù)器的資源,可以借鑒百度使用的方法:巧妙的利用meta的刷新作用,將http跳轉(zhuǎn)到https
可以基于http://dev.wangsl.com的虛擬主機(jī)路徑下寫(xiě)一個(gè)index.html,內(nèi)容就是http向https的跳轉(zhuǎn)
將下面的內(nèi)容追加到index.html首頁(yè)文件內(nèi)
[root@localhost ~]# cat /var/www/html/8080/index.html
<html>
<meta http-equiv="refresh" content="0;url=https://dev.wangsl.com/">
</html>
[root@localhost ~]# cat /usr/local/nginx/conf/vhosts/test.conf
server {
listen 80;
server_name dev.wangsl.com wangsl.com *.wangsl.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
#將404的頁(yè)面重定向到https的首頁(yè)
error_page 404 https://dev.wangsl.com/;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}四、通過(guò)proxy_redirec方式
解決辦法:
# re-write redirects to http as to https, example: /home proxy_redirect http:// https://;
以上就是Nginx強(qiáng)制跳轉(zhuǎn)Https(Http訪問(wèn)跳轉(zhuǎn)Https)的詳細(xì)內(nèi)容,更多關(guān)于Nginx強(qiáng)制跳轉(zhuǎn)Https的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- nginx強(qiáng)制使用https訪問(wèn)的方法(http跳轉(zhuǎn)到https)
- 詳解NGINX訪問(wèn)https跳轉(zhuǎn)到http的解決方法
- 如何通過(guò)nginx負(fù)載均衡跳轉(zhuǎn)https
- 使用nginx方式實(shí)現(xiàn)http轉(zhuǎn)換為https的示例代碼
- Nginx域名轉(zhuǎn)發(fā)https訪問(wèn)的實(shí)現(xiàn)
- Nginx將http轉(zhuǎn)換成https的詳細(xì)過(guò)程
- Nginx實(shí)現(xiàn)http自動(dòng)跳轉(zhuǎn)到https
- Nginx配置HTTP強(qiáng)制跳轉(zhuǎn)到HTTPS的解決辦法
- nginx配置將HTTPS請(qǐng)求轉(zhuǎn)換成HTTP的方法實(shí)現(xiàn)
- https如何通過(guò)nginx完成雙向認(rèn)證轉(zhuǎn)發(fā)
- nginx實(shí)現(xiàn)http轉(zhuǎn)換為https的項(xiàng)目實(shí)踐
相關(guān)文章
Nginx stub_status 監(jiān)控模塊的功能實(shí)現(xiàn)
本篇文章主要介紹了Nginx stub_status 監(jiān)控模塊的功能實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離
這篇文章主要為大家詳細(xì)介紹了nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
nginx反向代理配置400,404,502等狀態(tài)的自定義頁(yè)面問(wèn)題
這篇文章主要介紹了nginx反向代理配置400,404,502等狀態(tài)的自定義頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Nginx反向代理+DNS輪詢+IIS7.5 千萬(wàn)PV 百萬(wàn)IP 雙線 網(wǎng)站架構(gòu)案例
某公司有一站點(diǎn),一天IP 430W,PV 3100W,之前采用5臺(tái) DELL R610 做NLB,系統(tǒng)2008 IIS7.5.每天高峰期時(shí)都不堪重負(fù).會(huì)出現(xiàn)以下情況2012-11-11
Nginx配置同時(shí)支持http和https的兩種方式
現(xiàn)在的網(wǎng)站支持Https幾乎是標(biāo)配功能,Nginx能很好的支持Https功能,本文主要介紹了Nginx配置同時(shí)支持http和https的兩種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
nginx中狀態(tài)統(tǒng)計(jì)的實(shí)現(xiàn)
本文主要介紹了nginx中狀態(tài)統(tǒng)計(jì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Centos 6.5 64位中Nginx詳細(xì)安裝部署教程
Nginx是一個(gè)web服務(wù)器也可以用來(lái)做負(fù)載均衡及反向代理使用,目前使用最多的就是負(fù)載均衡,具體簡(jiǎn)介我就不介紹了百度一下有很多,下面直接進(jìn)入安裝步驟,需要的朋友可以參考下2017-08-08

