Nginx Rewrite相关功能-判断文件是否存在

            Nginx Rewrite相关功能-判断文件是否存在

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.访问指定域名的web服务器路径不存在时直接向用户报错

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
   http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"re
sponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

3>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/{static,images}
mkdir: created directory ‘/yinzhengjie/data/web/nginx’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/static’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/images’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/images/jd
mkdir: created directory ‘/yinzhengjie/data/web/nginx/images/jd’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wget https://www.jd.com/favicon.ico -O /yinzhengjie/data/web/nginx/images/jd/favicon.ico
--2019-12-24 15:15:42--  https://www.jd.com/favicon.ico
Resolving www.jd.com (www.jd.com)... 220.194.105.131, 2408:8710:20:1140:8000::3
Connecting to www.jd.com (www.jd.com)|220.194.105.131|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25214 (25K) [image/x-icon]
Saving to: ‘/yinzhengjie/data/web/nginx/images/jd/favicon.ico’

100%[===========================================================================================================>] 25,214      --.-K/s   in 0.001s  

2019-12-24 15:15:42 (22.4 MB/s) - ‘/yinzhengjie/data/web/nginx/images/jd/favicon.ico’ saved [25214/25214]

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "<h1>这是node101.yinzhengjie.org.cn的首页<h1>"  > /yinzhengjie/data/web/nginx/static/index.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx -R
/yinzhengjie/data/web/nginx:
total 0
drwxr-xr-x 3 root root 16 Dec 24 15:15 images
drwxr-xr-x 2 root root 24 Dec 24 15:16 static

/yinzhengjie/data/web/nginx/images:
total 0
drwxr-xr-x 2 root root 25 Dec 24 15:15 jd

/yinzhengjie/data/web/nginx/images/jd:
total 28
-rw-r--r-- 1 root root 25214 Mar 25  2016 favicon.ico

/yinzhengjie/data/web/nginx/static:
total 4
-rw-r--r-- 1 root root 50 Dec 24 15:16 index.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

4>.启动nginx

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:80                                                         *:*                  
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                            *:443                                                        *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 

5>.浏览器访问"https://node101.yinzhengjie.org.cn/"首页

6>.浏览器访问一个不存在的路径,例如"https://node101.yinzhengjie.org.cn/login/",结果直接向用户抛出404

 

二.访问指定域名的web服务器路径不存在时跳转到首页

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
       if ( !-f $request_filename ){
            rewrite (.*) https://node101.yinzhengjie.org.cn/index.html;
       }
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     24396     1  0 15:17 ?        00:00:00 nginx: master process nginx
nginx    24397 24396  0 15:17 ?        00:00:00 nginx: worker process
nginx    24398 24396  0 15:17 ?        00:00:00 nginx: worker process
nginx    24399 24396  0 15:17 ?        00:00:00 nginx: worker process
nginx    24400 24396  0 15:17 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     24396     1  0 15:17 ?        00:00:00 nginx: master process nginx
nginx    24510 24396  2 15:23 ?        00:00:00 nginx: worker process
nginx    24511 24396  1 15:23 ?        00:00:00 nginx: worker process
nginx    24512 24396  2 15:23 ?        00:00:00 nginx: worker process
nginx    24513 24396  2 15:23 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 

3>.浏览器访问一个不存在的路径,例如"https://node101.yinzhengjie.org.cn/login/"

 

posted @ 2019-12-22 01:41  尹正杰  阅读(1397)  评论(0编辑  收藏  举报