Nginx的https配置記錄以及http強制跳轉到https的方法梳理

一、Nginx安裝(略) 安裝的時候需要注意加上 --with-http_ssl_module,因為http_ssl_module不屬於Nginx的基本模塊。 Nginx安裝方法:

# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modulen# make && make installn

二、生成證書(略) 可以使用openssl生成證書: 可參考:cnblogs.com/kevingrace/ 比如生成如下兩個證書文件(假設存放路徑為/usr/local/nginx/cert/): wangshibo.crt wangshibo.key

三、修改Nginx配置

server {n listen 443;n server_name www.anzichen.com.cnn root /var/www/httpdocs/main/;nn ssl on;n ssl_certificate /usr/local/nginx/cert/wangshibo.crt;n ssl_certificate_key /usr/local/nginx/cert/wangshibo.key;n ssl_session_timeout 5m;n ssl_protocols SSLv2 SSLv3 TLSv1;n ssl_ciphers HIGH:!aNULL:!MD5; //或者是ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;n ssl_prefer_server_ciphers on;nn access_log /var/www/wwww.anzichen.com.cn/logs/clickstream_ssl.log main;n error_log /var/www/vhosts/www.anzichen.com.cn/logs/clickstream_error_ssl.log;nn if ($remote_addr !~ ^(124.165.97.144|133.110.186.128|133.110.186.88)) { //對訪問的來源ip做白名單限制n rewrite ^.*$ /maintence.php last;n }n location ~ .php$ {n fastcgi_pass 127.0.0.1:9000;n fastcgi_read_timeout 300;n fastcgi_index index.php;n fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;n #include fastcgi_params;n include fastcgi.conf;n }n}n

---------------------------------http訪問強制跳轉到https--------------------------------- 網站添加了https證書後,當http方式訪問網站時就會報404錯誤,所以需要做http到https的強制跳轉設置.

---------------一、採用nginx的rewrite方法---------------------

1) 下面是將所有的http請求通過rewrite重寫到https上。 例如將所有的m.anzichen.com.cn域名的http訪問強制跳轉到https。 下面配置均可以實現:

配置1:

server {n listen 80;n server_name m.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn rewrite ^(.*)$ https://$host$1 permanent; //這是ngixn早前的寫法,現在還可以使用。nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n


上面的跳轉配置rewrite ^(.*)$ https://$host$1 permanent; 也可以改為下面

rewrite ^/(.*)$ http://www.anzichen.com.cn/$1 permanent;n

或者

rewrite ^ http://www.anzichen.com.cn$request_uri? permanent;n


配置2:

server {n listen 80;n server_name m.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn return 301 https://$server_name$request_uri; //這是nginx最新支持的寫法nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n

配置3:這種方式適用於多域名的時候,即訪問anzichen.com.cn的http也會強制跳轉到https://m.anzichen.com.cn上面

server {n listen 80;n server_name m.anzichen.com.cnanzichen.com.cn *.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn if ($host ~* "^www.anzichen.com.cn$") {n rewrite ^/(.*)$ https://m.anzichen.com.cn/ permanent;n }nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n

配置4:下面是最簡單的一種配置

server {n listen 80;n server_name m.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn if ($host = "m.anzichen.com.cn") {n rewrite ^/(.*)$ http://m.anzichen.com.cn permanent;n }nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n

---------------二、採用nginx的497狀態碼---------------------

497 - normal request was sent to HTTPS 解釋:當網站只允許https訪問時,當用http訪問時nginx會報出497錯誤碼

思路: 利用error_page命令將497狀態碼的鏈接重定向到https://dev.wangshibo.com這個域名上

配置實例: 如下訪問m.anzichen.com或者anzichen.com的http都會被強制跳轉到https

server {n listen 80;n server_name m.anzichen.com.cnanzichen.com.cn *.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn error_page 497 https://$host$uri?$args; nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n

也可以將80和443的配置放在一起:

server { n listen 127.0.0.1:443; #ssl埠 n listen 127.0.0.1:80; #用戶習慣用http訪問,加上80,後面通過497狀態碼讓它自動跳到443埠 n server_name m.anzichen.com.cn; n #為一個server{......}開啟ssl支持 n ssl on; n #指定PEM格式的證書文件 n ssl_certificate /etc/nginx/wangshibo.pem; n #指定PEM格式的私鑰文件 n ssl_certificate_key /etc/nginx/wangshibo.key; nn #讓http請求重定向到https請求 n error_page 497 https://$host$uri?$args; nn location ~ / {n root /var/www/html/8080;n index index.html index.php index.htm;n }n }n

---------------三、利用meta的刷新作用將http跳轉到https---------------------

上述的方法均會耗費伺服器的資源,可以借鑒百度使用的方法:巧妙的利用meta的刷新作用,將http跳轉到https 可以基於http://dev.wangshibo.com的虛擬主機路徑下寫一個index.html,內容就是http向https的跳轉

將下面的內容追加到index.html首頁文件內

[root@localhost ~]# cat /var/www/html/8080/index.htmln<html> n<meta http-equiv="refresh" content="0;url=https://dev.wangshibo.com/"> n</html>nn[root@localhost ~]# cat /usr/local/nginx/conf/vhosts/test.confnserver {n listen 80;n server_name m.anzichen.com.cnanzichen.com.cn *.anzichen.com.cn;n index index.html index.php index.htm;nn access_log /usr/local/nginx/logs/8080-access.log main;n error_log /usr/local/nginx/logs/8080-error.log;nn #將404的頁面重定向到https的首頁 n error_page 404 https://m.anzichen.com.cn/; nn location ~ / {n root /var/www/html/8080; n index index.html index.php index.htm;n }n }nn****n


下面是nginx反代tomcat,並且http強制跳轉至https。 訪問http://zrx.wangshibo.com和訪問http://172.29.34.33:8080/zrx/結果是一樣的

···

[root@BJLX_34_33_V vhosts]# cat zrx.confnserver {n listen 80;n server_name m.anzichen.com.cn;n index index.html index.php index.htm;nn access_log logs/access.log;n error_log logs/error.log;nn return 301 https://$server_name$request_uri; nn location ~ / {n root /data/nginx/html;n index index.html index.php index.htm;n }n }nnn[root@BJLX_34_33_V vhosts]# cat ssl-zrx.confnupstream tomcat8 {n server 172.29.34.33:8080 max_fails=3 fail_timeout=30s;n}nnserver {n listen 443;n server_name m.anzichen.com.cn;n ssl on;nn ### SSL log files ###n access_log logs/ssl-access.log;n error_log logs/ssl-error.log;nn### SSL cert files ###n ssl_certificate ssl/www.anzichen.com.cn.cer; n ssl_certificate_key ssl/www.anzichen.com.cn.key; n ssl_session_timeout 5m;nn location / {n proxy_pass http://tomcat8/zrx/; n proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;n proxy_set_header Host $host;n proxy_set_header X-Real-IP $remote_addr;n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;n proxy_set_header X-Forwarded-Proto https;n proxy_redirect off;n}n}n

---------------四、通過proxy_redirec方式---------------------

解決辦法:

# re-write redirects to http as to https, example: /homenproxy_redirect http:// https://; n

推薦閱讀:

如何看待中國沃通wosign偷偷收購自己的根CA startcom並且簽發github.com的證書?
SSL 證書服務,大家用哪家的?
SSL中,公鑰、私鑰、證書的後綴名都是些啥?
瀏覽器如何驗證HTTPS證書的合法性?
為什麼12306的CA證書SRCA,CRL分發點是個區域網地址?

TAG:Nginx | PHP | SSL证书 |