咱們用nginx實現301跳轉,下面咱們先講一下實現的大概思想,首先咱們用yum或者編譯安裝nginx,而後配置nginx的主配置文件的子文件,(在配置子文件的時候能夠把默認文件先註釋掉)配置好子文件以後重啓nginx服務器。而後就能夠去測試你想要的結果啦。(前提是把防火牆關掉,例如:selinux、firewalld、iptables等一切的環境)php
第一步:清理環境html
1:首先查看firewalld的狀態linux
# systemctl status friewalldnginx
沒有關閉,要關閉firewalld數據庫
systemctl disable firewalldvim
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)