Nginx配置ssl證書
省略nginx安裝部分以及證書申請部分(可以參考“Apache配置https證書並跳轉”)
1、先配置好nginx網站環境。
2、配置網站的虛擬主機,保證網站可以通過http訪問。
3、再配置https訪問(記得上傳域名證書)。
4、將所有http訪問重定向到https訪問。
1、nginx的具體配置這裏不做解釋。
2、配置虛擬主機。
因通過yum安裝的nginx的配置文件中默認開啓了在其他文件路徑中進行虛擬主機配置的選項。
include /etc/nginx/conf.d/*.conf;
所有我們在 include /etc/nginx/conf.d/ 路徑下寫虛擬主機配置,
虛擬主機的名字自行設置,一般建議以域名的名稱命名:
配置如下:
server { listen 80; server_name www.zhanx.wang zhanx.wang; #網站域名 } location ~ \.php$ { root /data/discuz; #網站文件路徑 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location / { root /data/discuz; #網站文件路徑 index index.php; } }
3、配置https訪問,該配置也在虛擬主機所在路徑下進行配置即可:
配置如下:
# HTTPS server # server { listen 443 ssl; server_name www.zhanx.wang; ssl_certificate /data/nginx/1_zhanx.wang_bundle.crt; ssl_certificate_key /data/nginx/2_zhanx.wang.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /data/discuz; index index.php index.html index.htm; } }
4、配置nginx的301重定向跳轉
配置如下:
server { listen 80; server_name www.zhanx.wang zhanx.wang; if ($server_port = 80) { # http強制跳轉https return 301 https://www.zhanx.wang; } location ~ \.php$ { root /data/discuz; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location / { root /data/discuz; index index.php; } }
下面補充幾種nginx的重定向配置方式如下:
常見的301跳轉設置方法:
一、
if ($scheme = http ) { return 301 https://$host$request_uri; }
二、
server_name 8jieke.com ; rewrite ^(.*) https://8jieke.com$1 permanent
三、
if ($server_port = 80 ) { return 301 https://$host$request_uri; }
四、
server_name 8jieke.com ;return 301 https://$server_name$request_uri;
yum安裝的nginx在主配置文件中修改的配置方式如下圖所示: