Nginx配置HTTP強(qiáng)制跳轉(zhuǎn)到HTTPS的解決辦法
前言
https 訪問我們的測試域名 https://www.xxx.com 站點,但是當(dāng)我們直接在瀏覽器地址欄中直接輸入 www.xxx.com 的時候卻發(fā)現(xiàn)進(jìn)入的是 http 協(xié)議的網(wǎng)站,這與我們的初衷不一致。
由于瀏覽器默認(rèn)訪問域名使用的是80端口,而當(dāng)我們使用SSL證書后,網(wǎng)站的端口就變成了443,所以當(dāng)我們直接在瀏覽器中輸入網(wǎng)址 www.xxx.com 的時候進(jìn)入的是 80 端口的 HTTP 站點而不是 443 端口的 HTTPS 站點。
解決方法
這里提供兩種 http 跳轉(zhuǎn)到 https 的方法:
1. 使用nginx的 rewrite 將請求過來的 http URL直接重寫成 https
server {
listen 80;
#填寫綁定證書的域名
server_name?www.xxx.com;
#強(qiáng)制將http的URL重寫成https
rewrite ^(.*) https://$server_name$1 permanent;
}2. 使用301重定向的方式將 http 的請求重定向到 https 上
server {
listen 80;
#填寫綁定證書的域名
server_name?www.xxx.com;
#把http的域名請求轉(zhuǎn)成https
return 301 https://$host$request_uri;
}3.完整的代碼
# https配置
server {
listen 443 ssl;
server_name?www.xxx.com?xxx.com;
#ssl on; #開啟ssl支持
ssl_certificate?/data/ssl/www.xxx.com.pem; #指定服務(wù)器證書路徑
ssl_certificate_key?/data/ssl/www.xingguangshe.com.key; #指定私鑰證書路徑
ssl_session_timeout 5m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
root?/myweb/new/xxx.com;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php/?.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_pass php-fpm; #uquq use this
}
location ~ /\.ht {
deny all;
}
}
#http配置
server {
listen 80;
#listen somename:8080;
server_name?www.xxx.com?xxx.com;
client_max_body_size 80m;
#error_page 404 /data/ymg280/404.html;
#error_page 500 502 503 504 /errors/default/50x.html;
rewrite ^(.*) https://$server_name$1 permanent;
if ($host != 'ww.xxx.com'){
#rewrite ^/(.*)$?http://www.xxx.com/$1?permanent;
}
root?/myweb/new/xxx.com;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php/?.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_pass php-fpm;
}
#deny access to .htaccess files, if Apache's document root
location ~ /\.ht {
deny all;
}
}總結(jié)
到此這篇關(guān)于Nginx配置HTTP強(qiáng)制跳轉(zhuǎn)到HTTPS的文章就介紹到這了,更多相關(guān)Nginx強(qiáng)制跳轉(zhuǎn)HTTPS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nginx強(qiáng)制使用https訪問的方法(http跳轉(zhuǎn)到https)
- 詳解NGINX訪問https跳轉(zhuǎn)到http的解決方法
- 如何通過nginx負(fù)載均衡跳轉(zhuǎn)https
- 使用nginx方式實現(xiàn)http轉(zhuǎn)換為https的示例代碼
- Nginx域名轉(zhuǎn)發(fā)https訪問的實現(xiàn)
- Nginx將http轉(zhuǎn)換成https的詳細(xì)過程
- Nginx實現(xiàn)http自動跳轉(zhuǎn)到https
- nginx配置將HTTPS請求轉(zhuǎn)換成HTTP的方法實現(xiàn)
- Nginx強(qiáng)制跳轉(zhuǎn)Https(Http訪問跳轉(zhuǎn)Https)
- https如何通過nginx完成雙向認(rèn)證轉(zhuǎn)發(fā)
- nginx實現(xiàn)http轉(zhuǎn)換為https的項目實踐
相關(guān)文章
nginx服務(wù)器中access_log日志分析與配置詳解
通過訪問日志,可以知曉用戶的地址,網(wǎng)站的哪些部分最受歡迎,用戶的瀏覽時間,對大多數(shù)用戶用的的瀏覽器做出針對性優(yōu)化。下面這篇文章主要給大家介紹了關(guān)于nginx服務(wù)器中access_log日志分析與配置的相關(guān)資料,需要的朋友可以參考下。2017-12-12
在Linux中查看Apache或Nginx服務(wù)狀態(tài)的詳細(xì)步驟
在Linux中,查看Apache或Nginx服務(wù)的狀態(tài)通常涉及到使用系統(tǒng)管理工具或特定于這些Web服務(wù)器的命令,以下是如何查看Apache和Nginx服務(wù)狀態(tài)的詳細(xì)步驟,需要的朋友可以參考下2024-03-03
nginx運(yùn)行報錯:unknown directive “stream“的解決方案
這篇文章主要給大家介紹了nginx 運(yùn)行報錯:unknown directive "stream"的原因,主要是因為沒有安裝stream模塊導(dǎo)致的,我們只需要編譯安裝一下stream模塊即可解決這個問題,文中有詳細(xì)的解決方案,需要的朋友可以參考下2023-09-09
分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置實例)
這篇文章主要介紹了分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置實例),本文先是講解了分析需要屏蔽日志的方法,然后講解了Nginx中屏蔽IP的配置方法,需要的朋友可以參考下2015-02-02
nginx有哪些常規(guī)調(diào)優(yōu)手段詳解
性能調(diào)優(yōu)就是用更少的資源提供更好的服務(wù),成本利益最大化,下面這篇文章主要給大家介紹了關(guān)于nginx有哪些常規(guī)調(diào)優(yōu)手段的相關(guān)資料,需要的朋友可以參考下2023-01-01
解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時斜線‘/‘導(dǎo)致404問題
這篇文章主要介紹了解決Nginx location中配置proxy_pass轉(zhuǎn)發(fā)時斜線‘/‘導(dǎo)致404問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

