今天有位客戶問ytkah在nginx服務器如何設置http 301重定向到https,其實不難。他的服務器安裝寶塔面板了,更好操作了。進入站點設置的配置文件,如下圖所示,在第11行左右加入跳轉代碼
#301-START if ($host ~ '^abc.com'){ return 301 https://www.abc.com/$request_uri; } #301-END #301-START if ( $scheme = http ){ return 301 https://$server_name$request_uri; #或return 301 https://www.abc.com/$request_uri; } #301-END
另外一種方法是直接在nginx配置文件里改,一般是在會在 /usr/local/nginx/conf/nginx.conf
server { listen 80; ... return 301 https://$server_name$request_uri; } server { listen 443; ... }