Nginx-rewrite-生产案例实践

Nginx-学习目录

1、根据用户浏览器请求头中携带的语言调度到不同的页面

1.1、代码目录准备

]# tree /opt/code/
/opt/code/
├── en
│   └── index.html
└── zh
    └── index.html

1.2、配置nginx

cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  if ($http_accept_language ~* "zh-Ch|zh") {
    set $language /zh;
  }
  if ($http_accept_language ~* "en") {
    set $language /en;
  }
  rewrite ^/$ /$language;
  
  location / {
    index index.html;
  }
}
EOF

systemctl restart nginx

1.3、测试访问

~]# curl -L -H "accept-language: en"  http://url.cyc.com/
en

~]# curl -L -H "accept-language: zh"  http://url.cyc.com/
zh

2、用户通过手机设备访问url.cyc.com,跳转至url.cyc.com/m

2.1、代码准备

mkdir /opt/code/m
echo "url.cyc.com/m" >/opt/code/m/index.html

2.2、配置nginx

cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  if ($http_user_agent ~* "android|iphone|ipad") {
    rewrite ^/$ /m;
  }
}
EOF

systemctl restart nginx

2.3、测试访问

~]# curl -L -H "User-Agent: android"  http://url.cyc.com
url.cyc.com/m

3、用户通过手机设备访问url.cyc.com 跳转至m.cyc.com

3.1、代码准备

mkdir /opt/code/m
echo "m.cyc.com" >/opt/code/m/index.html

3.2、配置nginx

cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  if ($http_user_agent ~* "android|iphone|ipad") {
    rewrite ^/$ http://m.cyc.com;
  }
}
server {
  listen 80;
  server_name m.cyc.com;
  root /opt/code/m;
  index index.html;
}
EOF

systemctl restart nginx

3.3、测试访问

# 配置hosts
192.168.10.4 m.cyc.com
192.168.10.4 url.cyc.com

~]# curl -L -H "User-Agent: android"  http://url.cyc.com
m.cyc.com

4、用户通过http协议请求,能自动跳转至https协议

这里由于没有配置https,暂时写伪代码配置,熟悉语法即可 

4.1、nginx配置【rewrite】

cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  rewrite ^(.*)$ https://$server_name$1 redirect;
}
EOF

4.2、nginx配置【return】

cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  return 302 https://$server_name$request_uri;
}
EOF

5、网站在维护过程中,希望用户访问所有网站重定向至一个维护页面

5.1、nginx配置

echo "正在维护中" >/opt/code/wh.html
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  rewrite ^(.*)$ /wh.html break;
  location / {
    index index.html;
  }
}
EOF
systemctl restart nginx

5.2、测试访问

~]# curl -L url.cyc.com
正在维护中

6、当服务器遇到403 403 502等错误时,自动转到临时维护的静态页

6.1、nginx配置

echo "404  403 502,正在维护中" >/opt/code/wh.html
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  location / {
    index index.html;
  }
  error_page 404 403 502 = @tempdown;
  location @tempdown {
    rewrite ^(.*)$ /wh.html break;
  }
}
EOF
systemctl restart nginx

6.2、测试访问

]# curl -L url.cyc.com/asdfasdfasdf/asdfasdf
404  403 502,正在维护中

7、公司网站在停机维护时,指定的IP能够正常访问,其它的IP跳转到维护页

7.1、配置nginx

echo "404  403 502,正在维护中" >/opt/code/wh.html
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
  listen 80;
  server_name url.cyc.com;
  root /opt/code;
  set $ip 0;
  if ($remote_addr = '192.168.10.8') {
    set $ip 1;
    return 200 '正常访问';
  }
  if ($ip = 0){
    rewrite ^(.*)$ /wh.html break;
  }
  
  location /{
    root /opt/code;
    index index.html;
  }
}
EOF
systemctl restart nginx

8、将phpMyAdmin合成一个URL

8.1、需求1-格式:phpadmin.cyc.com

cat >/etc/nginx/conf.d/phpadmin.cyc.com.conf<<'EOF'
upstream phpadmin {
  server 192.168.10.5:8000;
  server 192.168.10.7:8000;
}
server {
  listen 80;
  server_name phpadmin.cyc.com;
  root /opt/code/m;
  index index.html;
  location / {
    proxy_pass http://phpadmin;
  }
}
EOF
systemctl restart nginx

8.2、需求2-格式:cyc.com/phpadmin

8.2.1、phpMyAdmin-nginx配置

cat >/etc/nginx/conf.d/phpadmin.cyc.com.conf<<'EOF'
upstream phpadmin {
  server 192.168.10.5:8000;
  server 192.168.10.7:8000;
}
server {
  listen 80;
  server_name cyc.com;
  root /opt/code;
  
  location ^~ /phpadmin {
     proxy_pass http://phpadmin;
     rewrite /phpadmin/(.*) /$1 break;
  }
  
  location / {
    try_files $uri $uri/ /index.php;
    root /opt/code;
    index index.html index.php;
  }
}
EOF
systemctl restart nginx

8.2.2、测试访问

posted @ 2023-05-02 17:13  小粉优化大师  阅读(14)  评论(0编辑  收藏  举报