一步一步教你Nginx地址重写

时间:2023-02-25 22:00:03

Nginx地址重写

一、在 Centos01 上安装安装 Nginx,设置网站根目录/www 使用域名 www.stz.com 访问,Cent02 上安装 DNS 服务解析 www.stz.com 和 www.stz.com.cn 以及 www.stz.com.en 域名

1、在Centos01上安装nginx依赖

1)挂载系统光盘配置本地yum仓库

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*

2)安装依赖程序

[root@centos01 ~]# yum -y install pcre-devel zlib-devel

3)创建管理Nginx用户

[root@centos01 ~]# useradd -M -s /sbin/nologin nginx
[root@centos01 ~]# umount /mnt/

2、配置安装nginx服务

1)切换光盘

一步一步教你Nginx地址重写

2)挂载光盘解压配置nginx

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# tar zxf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos01 src]# cd /usr/src/nginx-1.6.0/
[root@centos01 src]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

3)编译安装

[root@centos01 nginx-1.6.0]# make && make install

4)优化nginx命令检查配置文件

[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# nginx -t
nginx: the configuration file
/usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file
/usr/local/nginx/conf/nginx.conf test is successful

3、设置nginx服务器网站根目录设置主页修改配置文件

1)创建网站根目录设置主页

[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.stz.com" > /www/index.html

2)修改nginx主配置文件

[root@centos01 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
}

3)启动nginx服务监听端口

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3763/nginx: master

4、在Centos02上安装DNS服务,创建解析域名www.stz.com和www.stz.com.cn以及www.stz.com.en

1)挂载光盘安装DNS服务

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm

2)修改DNS服务主配置文件

[root@centos02 ~]# vim /etc/named.conf
options {
listen-on port 53 { 192.168.100.20; };
directory "/var/named";
};
zone "stz.com" IN {
type master;
file "/var/named/stz.com.zone";
};
zone "stz.com.cn" IN {
type master;
file "/var/named/stz.com.cn.zone";
};
zone "stz.com.en" IN {
type master;
file "/var/named/stz.com.en.zone";
};
[root@centos02 ~]# named-checkconf /etc/named.conf

4)修改DNS区域配置文件

[root@centos02 ~]# vim/var/named/stz.com.zone
$TTL 86400
@ SOA stz.com. root.stz.com. (
2023022312
1H
15M
1W
1D
)
@ NS centos02.stz.com.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# named-checkzone stz.com /var/named/stz.com.zone 
zone stz.com/IN: loaded serial 2023022312 OK
[root@centos02 ~]# vim/var/named/stz.com.cn.zone
$TTL 86400
@ SOA stz.com.cn. root.stz.com.cn. (
2023022312
1H
15M
1W
1D
)
@ NS centos02.stz.com.cn.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# named-checkzone stz.com.cn /var/named/stz.com.cn.zone
zone stz.com.cn/IN: loaded serial 2023022312 OK
[root@centos02 ~]# vim /var/named/stz.com.en.zone
$TTL 86400
@ SOA stz.com.en. root.stz.com.en. (
2023022312
1H
15M
1W
1D
)
@ NS centos02.stz.com.en.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# named-checkzone stz.com.en /var/named/stz.com.en.zone 
zone stz.com.en/IN: loaded serial 2023022312
OK

5)启动DNS服务开机自动启动

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

6)客户端测试DNS服务器

配置ip

一步一步教你Nginx地址重写

一步一步教你Nginx地址重写

7)客户端访问

一步一步教你Nginx地址重写

二、配置网站跳转使用if和set判断,判断用户输入域名http://www.stz.com/cn跳转到http://www.stz.com.cn,判断用户输入域名http://www.stz.com/en跳转到英文网站http://www.stz.com.en

1、创建中文网站和英文网站设置主页

1)设置中文网站根目录

[root@centos01 ~]# mkdir /www/cn
[root@centos01 ~]# echo "cn" > /www/cn/index.html

2)设置英文网站根目录

[root@centos01 ~]# mkdir /www/en
[root@centos01 ~]# echo "en" > /www/en/index.html

3)列出网站根目录数据

[root@centos01 ~]# ls -R /www/
/www/:
cn
en index.html

/www/cn:
index.html

/www/en:
index.html

2、修改nginx主配置文件当用户输入cn跳转中文网站输入en跳转英文网站

1)修改主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.stz.com.cn;
charset utf-8;

root /www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.stz.com.cn;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
set $language 'cn';
rewrite ^/$ http://www.stz.com/$language;
}
}
server {
listen 80;
server_name www.stz.com.en;
charset utf-8;
access_log logs/www.stz.com.access.log;

location / {
set $language 'en';
rewrite ^/$ http://www.stz.com/$language;
}
}
}

2)重新启动nginx服务

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3、客户端验证访问

1)客户端访问中文网站

一步一步教你Nginx地址重写

2)客户端访问英文网站

一步一步教你Nginx地址重写

4、优化网站跳转

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}

server {
listen 80;
server_name www.stz.com.cn www.stz.com.en;
charset utf-8;

location / {
if ($http_host ~ (cn)$) {
set $language 'cn';
rewrite ^/$ http://www.stz.com/$language;
}
if ($http_host ~ (en)$) {
set $language 'en';
rewrite ^/$ http://www.stz.com/$language;
}
}
}
}

2)重启Nginx服务

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3)访问中文网站

一步一步教你Nginx地址重写

4)访问英文网站

一步一步教你Nginx地址重写

三、return当用输入http://www.stz.com.cn给用户提示404错误,实现当用户输入www.stz.com.en跳转到指定域名www.stz.com的网站

1、通过return当用输入http://www.stz.com.cn给用户提示404错误

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_connections 1024;
}
http {
include mime.types;
sendfile on;
keepalive_timeout 65;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.stz.com.cn;
charset utf-8;

location / {
if ($http_host = "www.stz.com.cn") {
return 404;
}
}
}
}

2)重新启动Nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3)客户端访问www.stz.com.cn返回404错误

一步一步教你Nginx地址重写

2、通过return实现当用户输入http://www.stz.com.en跳转到指定域名www.stz.com的网站

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.stz.com.en;
charset utf-8;

location / {
if ($http_host = "www.stz.com.en") {
rewrite ^/$ http://www.stz.com;
}
}
}
}

2)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3)输入域名跳转www.stz.com网站

一步一步教你Nginx地址重写

3、当用户输入www.stz.com.en后通过break跳出终止地址重写

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.stz.com.cn;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
if ($http_host = "www.stz.com.cn") {
return 404;
}
}
}
server {
listen 192.168.100.10:80;
server_name www.stz.com.en;
charset utf-8;
location / {
if ($http_host = "www.stz.com.en") {
break;
rewrite ^/$ http://www.stz.com;
}
}
}
}

4)客户端验证

一步一步教你Nginx地址重写

四、永久重定向应用,当用户输入域名www.stz.com.cn跳转到www.stz.com网站,临时重定向应用,当用户输入域名 www.bdqn.com.en 跳 转 到 www.stz.com 网站, laset 的 应 用 当 用 户 输 入 访 问 http://www.stz.com/cn 返回中文,客户端 IP 地址是 192.168.100.50 提示访 问页面,不是 192.168.100.50 访问报错

1、永久重定向应用,当用户输入域名www.stz.com.cn或者www.stz.com.en跳转到www.stz.com网站

1)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.stz.com.cn;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
rewrite ^/$ http://www.stz.com permanent;
}
}
}

2)重新启动服务

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3)验证301永久重定向

一步一步教你Nginx地址重写

2、临时重定向应用,当用户输入域名www.stz.com.cn或者www.stz.com.en跳转到www.stz.com网站

1)修改nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.stz.com.en;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
rewrite ^/$ http://www.stz.com redirect;
}
}
}

2)重新启动

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

3)客户端验证

一步一步教你Nginx地址重写

一步一步教你Nginx地址重写

3、laset的应用当用户输入访问www.stz.com.cn 跳转到www.stz.com,当用户输入www.stz.com.cn/en跳转到英文网站

1)修改主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.stz.com.cn;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
if ($http_host ~* (cn)$) {
rewrite ^/$ http://www.stz.com last;
}
location /en {
root /www/en/;
index index.html index.htm;
}
}
}
}

2)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
nginx: no process found
[root@centos01 ~]# nginx

3)客户端访问验证客户端输入的域名是www.bdqn.com.cn跳转到www.stz.com域名

 

一步一步教你Nginx地址重写

一步一步教你Nginx地址重写

4)输入www.stz.com.cn/en跳到英文网站

一步一步教你Nginx地址重写

4、维护网页跳转,当客户端是测试机192.168.100.50显示页面,非192.168.100.50主机访问报错

1)修改Nginx主配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.stz.com;
charset utf-8;
access_log logs/www.stz.com.access.log;
location / {
root /www/;
index index.html index.htm;
set $rewrite true;
if ($remote_addr = "192.168.100.50") {
set $rewrite fales;
}
if ($rewrite = true) {
rewrite (.+) /index.html;
}
location = /index.html {
root /www/error/;
}
}
}
}

3)重新启动

[root@centos01 ~]# killall -I -QUIT nginx
nginx: no process found
[root@centos01 ~]# nginx

3)非192.168.100.50主访问提示错误

一步一步教你Nginx地址重写

4)是192.168.100.50访问正常

一步一步教你Nginx地址重写