Nginx配置HTTP強制跳轉到HTTPS的解決辦法
前言
https 訪問我們的測試域名 https://www.xxx.com 站點,但是當我們直接在瀏覽器地址欄中直接輸入 www.xxx.com 的時候卻發(fā)現進入的是 http 協(xié)議的網站,這與我們的初衷不一致。
由于瀏覽器默認訪問域名使用的是80端口,而當我們使用SSL證書后,網站的端口就變成了443,所以當我們直接在瀏覽器中輸入網址 www.xxx.com 的時候進入的是 80 端口的 HTTP 站點而不是 443 端口的 HTTPS 站點。
解決方法
這里提供兩種 http 跳轉到 https 的方法:
1. 使用nginx的 rewrite 將請求過來的 http URL直接重寫成 https
server { listen 80; #填寫綁定證書的域名 server_name?www.xxx.com; #強制將http的URL重寫成https rewrite ^(.*) https://$server_name$1 permanent; }
2. 使用301重定向的方式將 http 的請求重定向到 https 上
server { listen 80; #填寫綁定證書的域名 server_name?www.xxx.com; #把http的域名請求轉成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; #指定服務器證書路徑 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; } }
總結
到此這篇關于Nginx配置HTTP強制跳轉到HTTPS的文章就介紹到這了,更多相關Nginx強制跳轉HTTPS內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Linux中查看Apache或Nginx服務狀態(tài)的詳細步驟
在Linux中,查看Apache或Nginx服務的狀態(tài)通常涉及到使用系統(tǒng)管理工具或特定于這些Web服務器的命令,以下是如何查看Apache和Nginx服務狀態(tài)的詳細步驟,需要的朋友可以參考下2024-03-03nginx運行報錯:unknown directive “stream“的解決方案
這篇文章主要給大家介紹了nginx 運行報錯:unknown directive "stream"的原因,主要是因為沒有安裝stream模塊導致的,我們只需要編譯安裝一下stream模塊即可解決這個問題,文中有詳細的解決方案,需要的朋友可以參考下2023-09-09分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置實例)
這篇文章主要介紹了分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置實例),本文先是講解了分析需要屏蔽日志的方法,然后講解了Nginx中屏蔽IP的配置方法,需要的朋友可以參考下2015-02-02解決Nginx location中配置proxy_pass轉發(fā)時斜線‘/‘導致404問題
這篇文章主要介紹了解決Nginx location中配置proxy_pass轉發(fā)時斜線‘/‘導致404問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05