中间件

Nginx路由配置

2019-03-04  本文已影响54人  任嘉平生愿
image.png
普通配置
#### 全局块 开始 ####

user nobody nobody;          # 配置允许运行Nginx服务器的用户和用户组

worker_processes 3;          # 配置允许Nginx进程生成的worker process数

error_log logs/error.log;      # 配置Nginx服务器运行时错误日志存放路径

pid nginx.pid;                    # 配置Nginx服务器运行时的pid文件存放路径和名称
##### 全局块 结束 #####
#### events块 开始 ####
events
{
        use epoll;      # 配置事件驱动模型

       worker_connections 1024;      # 配置最大连接数

}
#### events块 结束 ####
#### http块 开始 ####
http{

        include    mime.types;      #定义MIME-Type

        default_type application/octet-stream;      

        sendfile on;            #配置允许使用sendfile方式传输

        keepalive_time 65;        #配置连接超时时间
        
       log_format  access.log  '$remote_addr-[$time_local]-"$request"-"$http_user_agent" ';      #配置请求处理日志的格式

      #### server块 开始 ####
      ## 配置虚拟主机 myServer1
      server {
      
                listen 8081;  #配置监听端口和主机名称(基于名称)

                server_name  myServer1;

                access_log  /myweb/server1/log/access.log;  #配置请求处理日志存放路径

                error_page    404     /404.html;   #配置错误页

                # 配置处理/server1/location1请求的location
                location /server1/location1 {
                            root    /myweb;
                            index  index.ser1-loc1.htm;
               }
              # 配置处理/server1/location2请求的location
              location /server1/location2{
                          root /myweb;
                          index index.svr1-loc2.htm;
             }
      }
            
     ## 配置虚拟主机 myServer2
    server {

                listen    8082;
                server_name    192.169.1.3;
                access_log    /myweb/server2/log/access.log;
                error_page  404  /404.html;        #对错误页面404.html做了定向配置
                location /server2/location1 {
                      root      /myweb;
                      index    index.svr2-loc1.htm;
                }
                location  /svr2/loc2 {
                      alias     /myweb/server2/location2/;        #对location的URI进行更改
                      index    index.svr2-loc2.htm; 
               }
               location = /404.html {
                      root      /myweb/;
                      index  404.html;
              }
   }
   #### server块 结束 ####
}
#### http块 结束 ####

location 语法

=   = 开头表示精确匹配
^~  ^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
~   ~ 开头表示区分大小写的正则匹配
~*  ~* 开头表示不区分大小写的正则匹配
!~和!~*  !~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
/   用户所使用的代理(一般为浏览器)
$http_x_forwarded_for   可以记录客户端IP,通过代理服务器来记录客户端的ip地址
$http_referer   可以记录用户是从哪个链接访问过来的

rewrite 语法
http://rewrite.enjoy.com/aa.html --命中 /aa.html ,path=aa.html

本来应该在html/static文件夹下查找aa.html

image.png

被重写在html/static文件夹下查找a.html

上一篇 下一篇

猜你喜欢

热点阅读