nginx配置文件解答

nginx配置文件详解:

server {
    listen 80;
    servername    www.nginx1.com
    
    location / {
        root html;     数据路径
        index index.html index.htm
        }
用户访问www.nginx1.com,访问的是本机html下的目录内容

    location /test {
        root /data/www/zabbix;
        }
用户访问www.nginx1.com,访问时本机/data/www/zabbix 下的内容

    location /test {
        proxy_pass http://192.168.133.14/bbs;
    }
用户访问www.nginx1.com/test,会将访问的内容转发到后端192.168.133.14/bbs下。
如果要是用正则匹配的话,ip后面可以加端口,但是不能加其他的。
    location /test/ {
        proxy_pass http://192.168.133.14/bbs/;
    }
如上所示,格式必须匹配,访问到192.168.133.14/bbs/下。

如下采用正则方法
    location ~* ^/test {
        proxy_pass http://192.168.133.134;
    }
访问以test开头的路径,会访问到后端服务器www.xxx.com下的test文件下的内容,
做这种正则匹配的时候,proxy_pass后不能加路径,只能加ip或者域名。

    location /proxy/ {
        proxy_pass http://10.2.2.1:8080;
    }
访问http://127.0.0.1/proxy/cuffs,nginx匹配到/proxy/路径,
实际请求的服务器是http://10.2.2.1/proxy/cuffs.

~    区分大小写
~*  不区分大小写
^~    做逐字符匹配

wiki.nginx.org
http://xinklabi.iteye.com/blog/2207127

反向代理:

所有以.php结尾的文件都转发给127.0.0.1
location ~* .php${
    fastcgi_pass    127.0.0.1:9000;
}
事件驱动相关
http{

}

关于http相关的配置
server{
    listen 80;
    server_name www.magedu.com;
    
虚拟主机
    location [op] URI {
    proxy_pass        后端服务器
    proxy_set_header X-Real-IP $remote_addr;
    
    }
    }
op操作符
通常是匹配文件的
~    区分大小写
~*    不区分大小写,源自符匹配
^~    不做正则表达式,逐字符搜索
=    路径精确匹配

location /form/{
    proxy_pass http://172.10.2.100:8080/bbs/;
    }
访问http://www.magedu.com/forum/
   -->>http://172.10.2.100:8080/bbs/
当访问上面网站时候,转到下面的位置。



当location后面使用OP操作符时,不允许使用后面的/bbs/,保证pass后的ip后无路径
location ~* ^/form {
    proxy_pass http://175.23.10.1:8080;
    }
所有的URL路径以form开头的,都访问到form下
访问 http://www.magedu.com/forum
   --->>-->>http://172.10.2.100:8080/form





upstream 定义指令server之外。负载均衡。必须改变proxy_pass
upstream webserer {
    server 192.168.122.1 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.133.1 weight=2 max_fails=2 fail_timeout=2;
    server 192.13.5.1:8080 backup;使用该项会在前面两台服务器down的情况下,使用该台服务器。
    }
server {
    location / {
        proxy_pass http://webserer/;
        proxy_set_header X-Real-IP $remote_addr;
        }
        
        
        
缓存目录,子目录级别。
nginx:
    cache:共享内存,存储键和缓存对象元数据
            磁盘空间:存储数据
    proxy_cache_path: 不能放到server()里面。

proxy_cache_path /nginx/cache/first level=1:2 keys_zone=first:20m max_size=1G;
                缓存目录    两个目录,第一目录一个字节,第二个目录两个字节 内存区域大小20M 缓存最大1G
在location定义缓存,及其缓存名。

http{
    proxy_cache_path /nginx/cache/first level=1:2 keys_zone=first:20m max_size=1G;
}
location / {
    proxy_pass http://webservers;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_cache first;        该first是缓存的区域名,如果是off,则关闭缓存。
}




rewrite地址重写
location /images/ {
        rewrite http://126.23.2.1/images/
        }
访问www.magedu.com下的images将会重定向到http://126.23.2.1/images/


支持正则表达式
location / {
    root html;
    index index.html;
    rewrite "^/bbs/(.*)/images/(.*).jpg$" http://www.mangedu.com/bbs/$2/images/$1.jpg last;

}
访问www.magedu.com下的以bbs开头的文件下的a内容下的images下的b内容  (a,b分别代表任意内容)
将被重定向到该网址的/bbs/b/images/a/内容,如果跟上last
/URL/bbs/a/images/b --->>/URL/bbs/b/images/a/---->>/URL/bbs/a/images/b
last:代表本次重定向后再进行一轮检查
break:代表本次重写完成后,执行后续操作




原文地址:https://www.cnblogs.com/fengzhongzhuzu/p/8670407.html