Nginx

nginx常见用法

2020-07-22  本文已影响0人  WebGiser

环境:docker17.03.1-ce、nginx/1.18.0
nginx容器启动命令:

docker run -d -p 80:80
  --name nginx
  --privileged=true
  --restart=always
  -v /data2/geovis/docker-nginx/logs:/val/log/nginx
  -v /data2/geovis/docker_nginx/nginx_share_dir:/usr/share/nginx/html
  -v /data2/geovis/docker_nginx/nginx.conf:/etc/nginx/nginx.conf
  nginx

1、动静分离

1、静态网页
    location /SinglePage{
        alias /usr/share/nginx/html/SinglePage/;
        index index.html;
    }

测试:
(1)将静态网页复制到nginx映射的静态目录/data2/geovis/docker_nginx/nginx_share_dir下
(2)访问http://ip:80/SinglePage/地址,即可访问静态网页

2、文件目录索引
      location / {
            #代理本地文件夹
            root E:\\temp;
            autoindex on;

            #root /usr/share/nginx/html;   
            #autoindex on;

            #root   html;
            #index  index.html index.htm;
        }

测试:
(1)访问http://ip:80即可看到下面的文件目录列表

3、动静
      # 所有静态请求都由nginx处理,存放目录为html 
      location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {  
            root    e:\wwwroot;  
            #缓存时间1天 expires 1d;
        }  

        # 所有动态请求都转发给tomcat处理  
        location ~ \.(jsp|do)$ {  
            proxy_pass  http://test;  
        } 

2、代理服务

1、反向代理

Nginx 代理服务器:192.168.1.101
Nginx Web服务器:192.168.1.102

     upstream geovis{
        server 192.168.1.102:8080;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }

测试:
(1)访问:http://192.168.1.101/geovis/soujiu/#/earth,将会转发到 http://192.168.1.102:8080/soujiu/#/earth

3、负载均衡

Nginx 代理服务器:192.168.1.101
Nginx Web服务器1:192.168.1.102
Nginx Web服务器2:192.168.1.103

1、RR策略

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除

    upstream geovis{
        server 192.168.1.102:8080;
        server 192.168.1.103:8080;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }
2、权重策略

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

    upstream geovis{
        server 192.168.1.102:8080 weight=9;
        server 192.168.1.103:8080 weight=5;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }
3、ip_hash策略

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

    upstream geovis{
        ip_hash;
        server 192.168.1.102:8080;
        server 192.168.1.103:8080;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }
4、fair策略

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

    upstream geovis{
        fair;
        server 192.168.1.102:8080;
        server 192.168.1.103:8080;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }
5、url_hash策略
    upstream geovis{
        hash $request_uri; 
        hash_method crc32;
        server 192.168.1.102:8080;
        server 192.168.1.103:8080;
    }
    location ^~/geovis/ {
        proxy_pass http://geovis/;
        proxy_set_header Host $http_host;
    }
上一篇 下一篇

猜你喜欢

热点阅读