运维

Nginx的使用

2021-04-27  本文已影响0人  felixfeijs

Nginx的使用

反向代理

server {  
        listen       80;                                                         
        server_name  localhost;                                               
        client_max_body_size 1024M;

        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host:$server_port;
        }
    }

负载均衡

RR(默认)模式
upstream test {
    server localhost:8080;
    server localhost:8081;
}
server {
    listen       81;                                                         
    server_name  localhost;                                               
    client_max_body_size 1024M;

    location / {
        proxy_pass http://test;
        proxy_set_header Host $host:$server_port;
    }
}
权重模式
upstream test {
    server localhost:8080 weight=9;
    server localhost:8081 weight=1;
}
ip_hash
upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}
fair(第三方)
upstream backend { 
    fair; 
    server localhost:8080;
    server localhost:8081;
} 
url_hash(第三方)
upstream backend { 
    hash $request_uri; 
    hash_method crc32; 
    server localhost:8080;
    server localhost:8081;
} 
总结

HTTP服务器(资源服务器)

server {
    listen       80;                                                         
    server_name  localhost;                                               
    client_max_body_size 1024M;

    location / {
           root   /usr/local/root;
           index  index.html;
       }
}
动静分离
upstream test{
server localhost:8080;
server localhost:8081;
}

server {  
      listen       80;  
      server_name  localhost;  

      location / {  
          root   /usr/local/root;  
          index  index.html;  
      }  

      # 所有静态请求都由nginx处理,存放目录为html  
      location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {  
          root    e:wwwroot;  
      }  

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

      error_page   500 502 503 504  /50x.html;  
      location = /50x.html {  
          root   e:wwwroot;  
      }  
  }

正向代理

resolver 114.114.114.114 8.8.8.8;server {

    resolver_timeout 5s;

    listen 81;

    access_log  e:wwwrootproxy.access.log;
    error_log   e:wwwrootproxy.error.log;

    location / {
        proxy_pass http://$host$request_uri;
    }
}

Nginx重新读取配置

nginx -s reload

上一篇 下一篇

猜你喜欢

热点阅读