我们用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)