nginx實現301(加密)跳轉和200跳轉


       我們用nginx實現301跳轉,下面我們先講一下實現的大概思想,首先我們用yum或者編譯安裝nginx,然后配置nginx的主配置文件的子文件,(在配置子文件的時候可以把默認文件先注釋掉)配置好子文件之后重啟nginx服務器。然后就可以去測試你想要的結果啦。(前提是把防火牆關掉,例如:selinux、firewalld、iptables等一切的環境)

第一步:清理環境

1:首先查看firewalld的狀態

# systemctl status friewalld

沒有關閉,要關閉firewalld

systemctl disable firewalld

2:查看selinux的狀態

# getenforce(顯示enforcing是開啟的狀態,顯示disable是關閉的狀態)

沒有關閉則編輯vim /etc/selinux/conf

把selinux=enforcing改成selinux=disabled(重啟后生效)

3:查看iptables的狀態

# systemctl status iptables

把iptables的規則全部清理掉

# iptables -F

# iptables -F -t nat

# iptables -F -t mangle

之后把iptables關閉

# systemctl stop iptables

# systemctl disable iptables

4:關閉networkmanager

# systemctl status NetworkManager

# systemctl stop NetworkManager

# systemctl disable NetworkManager

第二步yum安裝nginx

(首先要有epel源,下載epel源

# yum install -y epel-release

# yum makecache

)然后就可以下載nginx服務了

# yum install -y nginx

啟動nginx

# systemctl start nginx

第三步:配置nginx主配置文件

# vim /etc/nginx/nginx.conf

 

把include /etc/nginx/conf/*.conf下面的子文件都注釋掉,按wq保存退出

      之后進入/etc/nginx.conf,編輯一個子文件

例如:vim  1.conf

編輯好之后保存退出。

意思就是:當你訪問192.168.213.133時,網頁將會跳轉到https://www.taobao.com

第四步:測試

      用nginx -t 檢查文件有沒有編輯錯誤,再看一下80服務有沒有開啟,用losf -i:80判斷無誤之后, 重啟服務systemctl restart nginx。然后就可以在網頁上輸入自己設定的IP加端口號進行訪問了,這就實現了301加密跳轉。

實現200普通跳轉

步驟和上面的一樣就是配置文件不一樣,如果實現普通跳轉,還要加一步,就是編寫你要在網頁訪問到的內容

# cd /var/www/html

# vim 2.html

 編寫您要的內容

然后就可以在網頁上訪問啦。(在訪問的時候一定要加后面的目錄哦,例如:192.168.213.133/2.html)

 帶有域名的301跳轉  

 1)帶有數據庫有后台的網站實現301跳轉判斷

例:域名是5773.com , 網站是vns_hb.com,網站放到/wz這個文件夾的目錄下

# vim 1.conf

  server {

    listen      80 ;
    server_name  5773.com www.5773.com;
    charset      utf-8;

   location / {

    root                 /wz/vns_hb.com;
    index               index.html index.htm index.php;
    }

    error_page  404 /404.html;
    location =  /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

    location ~ \.php$ {
    root /wz/vns_hb.com;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

保存退出。

重啟一下nginx

# systemctl   reload  nginx

2)帶有證書的網站實現301跳轉的判斷

例:域名是1778.com ,網站是1778.com ,網站放到/wz文件夾的目錄下

# vim 1.conf

  server {
    listen 80 ;
    listen 443 ssl;
    server_name 1778.com www.1778.com;
    charset utf-8;
    index index.html;
    root /wz/1778.com;
    if ($scheme = http ) {
    return 301 https://$host$request_uri;
    }
    ssl_certificate_key /etc/nginx/conf/1778/Nginx/2_1778.com.key;
    ssl_certificate /etc/nginx/conf/1778/Nginx/1_1778.com_bundle.crt;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }
    location ~ \.php$ {
    root /wz/1778.com;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

保存退出

重啟一下nginx

# systemctl reload nginx

3)沒有證書沒有數據庫,但是輸入https還能正常跳轉  

例:域名是1655.com ,網站是1655.com , 存放網站目錄是/wz

# vim 1.conf

  server {
    listen 80 ;

    listen 443;
    server_name 1655.com www.1655.com;
    charset utf-8;
    #if ($scheme = https ) {
    return 301 http://$host$request_uri;
    #}

    location / {
    root /wz/1655.com;
    index index.html index.htm index.php;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

    location ~ \.php$ {
    root /wz/1655.com;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

 保存退出。

重啟一下nginx

# systemctl reload nginx

4)實現多域名跳轉

例:域名7788110.com~7788511.cc等域名 ,網站是7788990.com , 網站目錄/wz

# vim 1.conf

  server {

    listen  80;

    listen  443;

    charset  utf8;

    server_name 7788110.com www.7788110.com 7788220.com www.7788220.com 7788330.com www.7788330.com 7788440.com www.7788440.com 7788550.com www.7788550.com 7788660.com www.7788660.com 7788770.com www.7788770.com 7788880.cocm www.7788880.com 7788210.net www.7788210.net 7788660.net www.7788660.net 7788511.cc www.7788511.cc;

    root  /wz/7788990.com;

    index  index.html index.htm index.php;   

    # if ($server_port = 80 ) {
    return 301 https://www.7788990.com$request_uri;
    rewrite ^/[0-9a-zA-Z]+$ https://www.7788990.com/ break;
    # }

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }
    location ~ \.php$ {
    root /wz/7788990.com;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

保存退出。

重啟一下nginx

# systemctl reload nginx

5)實現用戶輸入域名后綴是ID和TYPE來訪問網站實現301跳轉

例:域名chnnowm.com ,要跳轉的網站是232xinyi.com , 

# vim 1.conf

  server {

    listen  80;

    charset  utf8;

    server_name  chnnowm.com www.chnnowm.com;

    index  index.html index.htm;

    if ($arg_typ = 'xytb' ) {
    return 301 https://232xinyi.com:8888/Reqister?a=$arg_ID;
    }
      ssl_certificate_key /etc/nginx/conf/chnnowm.com/Nginx/2_chnnowm.com.key;
      ssl_certificate/etc/nginx/conf/chnnowm/Nginx/1_chnnowm.com_bundle.crt;

    include /etc/nginx/default.d/*.conf;

 

    location / {
    }
    location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    }

保存退出。

重啟一下nginx

# systemctl reload nginx

讓問的時候直接域名+ID+TYPE(例如:https://www.chnnowm.com?ID=706787&type=xytb

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM