欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx強(qiáng)制跳轉(zhuǎn)Https(Http訪問跳轉(zhuǎn)Https)

 更新時(shí)間:2023年10月17日 09:55:26   作者:趙客縵胡纓v吳鉤霜雪明  
這篇文章主要為大家介紹了Http訪問強(qiáng)制跳轉(zhuǎn)到Https的幾種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Nginx安裝注意事項(xiàng)

最近公司要上線HTTPS,需要把之前的HTTP訪問強(qiáng)制跳轉(zhuǎn)到HTTPS

安裝的時(shí)候需要注意加上 --with-http_ssl_module,因?yàn)閔ttp_ssl_module不屬于Nginx的基本模塊。

#1.配置
./configure 
--user=www 
--group=www 
--prefix=/usr/local/nginx 
--with-http_stub_status_module 
--with-http_ssl_module
#2.編譯安裝
make && make install

配置SSL證書

如下兩個(gè)證書文件

ssl.crt

ssl.key

配置存放路徑為/usr/local/nginx/cert/

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

Http訪問強(qiáng)制跳轉(zhuǎn)到Https的幾種方式:

一、采用nginx的rewrite方法

1) 下面是將所有的http請(qǐng)求通過rewrite重寫到https上。

例如將所有的dev.wangsl.com域名的http訪問強(qiáng)制跳轉(zhuǎn)到https。

下面配置均可以實(shí)現(xiàn):

#配置1:
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
    rewrite ^(.*)$  https://$host$1 permanent;        //這是ngixn早前的寫法,現(xiàn)在還可以使用。
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
================================================================
上面的跳轉(zhuǎn)配置rewrite ^(.*)$  https://$host$1 permanent;
也可以改為下面
rewrite ^/(.*)$ http://dev.wangsl.com/$1 permanent;
或者
rewrite ^ http://dev.wangsl.com$request_uri? permanent;
================================================================
#配置2:
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
    return      301 https://$server_name$request_uri;      //這是nginx最新支持的寫法
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
#配置3:這種方式適用于多域名的時(shí)候,即訪問wangsl.com的http也會(huì)強(qiáng)制跳轉(zhuǎn)到https://dev.wangsl.com上面
server {
    listen 80;
    server_name dev.wangsl.com wangsl.com *.wangsl.com;
    index index.html index.php index.htm;
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
    if ($host ~* "^wangsl.com$") {
    rewrite ^/(.*)$ https://dev.wangsl.com/ permanent;
    }
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
#配置4:下面是最簡(jiǎn)單的一種配置
server {
    listen 80;
    server_name dev.wangsl.com;
    index index.html index.php index.htm;
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
    if ($host = "dev.wangsl.com") {
       rewrite ^/(.*)$ http://dev.wangsl.com permanent;
    }
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }

二、采用nginx的497狀態(tài)碼

497 - normal request was sent to HTTPS 
#解釋:當(dāng)網(wǎng)站只允許https訪問時(shí),當(dāng)用http訪問時(shí)nginx會(huì)報(bào)出497錯(cuò)誤碼
#配置實(shí)例:
#如下訪問dev.wangsl.com或者wangsl.com的http都會(huì)被強(qiáng)制跳轉(zhuǎn)到https
server {
    listen 80;
    server_name dev.wangsl.com wangsl.com *.wangsl.com;
    index index.html index.php index.htm;
    access_log  /usr/local/nginx/logs/8080-access.log main;
    error_log  /usr/local/nginx/logs/8080-error.log;
    error_page 497  https://$host$uri?$args; 
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }
#也可以將80和443的配置放在一起:
server { 
    listen       127.0.0.1:443;  #ssl端口 
    listen       127.0.0.1:80;   #用戶習(xí)慣用http訪問,加上80,后面通過497狀態(tài)碼讓它自動(dòng)跳到443端口 
    server_name  dev.wangsl.com; 
    #為一個(gè)server{......}開啟ssl支持 
    ssl                  on; 
    #指定PEM格式的證書文件  
    ssl_certificate      /etc/nginx/wangsl.pem;  
    #指定PEM格式的私鑰文件 
    ssl_certificate_key  /etc/nginx/wangsl.key; 
    #讓http請(qǐng)求重定向到https請(qǐng)求  
    error_page 497  https://$host$uri?$args; 
    location ~ / {
    root /var/www/html/8080;
    index index.html index.php index.htm;
    }
    }

三、利用meta的刷新作用將http跳轉(zhuǎn)到https

上述的方法均會(huì)耗費(fèi)服務(wù)器的資源,可以借鑒百度使用的方法:巧妙的利用meta的刷新作用,將http跳轉(zhuǎn)到https
可以基于http://dev.wangsl.com的虛擬主機(jī)路徑下寫一個(gè)index.html,內(nèi)容就是http向https的跳轉(zhuǎn) 

將下面的內(nèi)容追加到index.html首頁文件內(nèi)

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

四、通過proxy_redirec方式

解決辦法:

# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;

以上就是Nginx強(qiáng)制跳轉(zhuǎn)Https(Http訪問跳轉(zhuǎn)Https)的詳細(xì)內(nèi)容,更多關(guān)于Nginx強(qiáng)制跳轉(zhuǎn)Https的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Nginx stub_status 監(jiān)控模塊的功能實(shí)現(xiàn)

    Nginx stub_status 監(jiān)控模塊的功能實(shí)現(xiàn)

    本篇文章主要介紹了Nginx stub_status 監(jiān)控模塊的功能實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Nginx的正則表達(dá)式詳解

    Nginx的正則表達(dá)式詳解

    今天來聊一聊他的正則表達(dá)式的使用規(guī)則,我會(huì)簡(jiǎn)單的舉幾個(gè)例子然后進(jìn)行說明講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離

    nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離

    這篇文章主要為大家詳細(xì)介紹了nginx實(shí)現(xiàn)負(fù)載均衡和動(dòng)靜分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • nginx反向代理配置400,404,502等狀態(tài)的自定義頁面問題

    nginx反向代理配置400,404,502等狀態(tài)的自定義頁面問題

    這篇文章主要介紹了nginx反向代理配置400,404,502等狀態(tài)的自定義頁面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nginx反向代理+DNS輪詢+IIS7.5 千萬PV 百萬IP 雙線 網(wǎng)站架構(gòu)案例

    Nginx反向代理+DNS輪詢+IIS7.5 千萬PV 百萬IP 雙線 網(wǎng)站架構(gòu)案例

    某公司有一站點(diǎn),一天IP 430W,PV 3100W,之前采用5臺(tái) DELL R610 做NLB,系統(tǒng)2008 IIS7.5.每天高峰期時(shí)都不堪重負(fù).會(huì)出現(xiàn)以下情況
    2012-11-11
  • Nginx配置同時(shí)支持http和https的兩種方式

    Nginx配置同時(shí)支持http和https的兩種方式

    現(xiàn)在的網(wǎng)站支持Https幾乎是標(biāo)配功能,Nginx能很好的支持Https功能,本文主要介紹了Nginx配置同時(shí)支持http和https的兩種方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • nginx中狀態(tài)統(tǒng)計(jì)的實(shí)現(xiàn)

    nginx中狀態(tài)統(tǒng)計(jì)的實(shí)現(xiàn)

    本文主要介紹了nginx中狀態(tài)統(tǒng)計(jì)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 詳細(xì)nginx多域名配置的方法

    詳細(xì)nginx多域名配置的方法

    Nginx綁定多個(gè)域名,可通過把多個(gè)域名規(guī)則寫一個(gè)配置文件里實(shí)現(xiàn),也可通過分別建立多個(gè)域名配置文件實(shí)現(xiàn),為了管理方便,建議每個(gè)域名建一個(gè)文件,有些同類域名則可寫在一個(gè)總的配置文件里。下面這篇文章就來詳細(xì)看看nginx多域名配置的方法,有需要的朋友們可以參考。
    2016-12-12
  • Centos 6.5 64位中Nginx詳細(xì)安裝部署教程

    Centos 6.5 64位中Nginx詳細(xì)安裝部署教程

    Nginx是一個(gè)web服務(wù)器也可以用來做負(fù)載均衡及反向代理使用,目前使用最多的就是負(fù)載均衡,具體簡(jiǎn)介我就不介紹了百度一下有很多,下面直接進(jìn)入安裝步驟,需要的朋友可以參考下
    2017-08-08
  • 深入淺析nginx部署及簡(jiǎn)單優(yōu)化

    深入淺析nginx部署及簡(jiǎn)單優(yōu)化

    Nginx是lgor Sysoev為俄羅斯訪問量第二的rambler.ru站點(diǎn)設(shè)計(jì)開發(fā)的。本文重點(diǎn)給大家介紹nginx部署及簡(jiǎn)單優(yōu)化方案,感興趣的朋友一起看看吧
    2018-08-08

最新評(píng)論