nginx的配置

反向代理

proxy_pass 将请求转发到有处理能力的端上,默认不会转发请求中的 Host 头部

location /blog {
    prox_pass http://localhost:9000;

    ### 下面都是次要关注项
    proxy_set_header Host $host;
    proxy_method POST;
    # 指定不转发的头部字段
    proxy_hide_header Cache-Control;
    proxy_hide_header Other-Header;
    # 指定转发的头部字段
    proxy_pass_header Server-IP;
    proxy_pass_header Server-Name;
    # 是否转发包体
    proxy_pass_request_body on | off;
    # 是否转发头部
    proxy_pass_request_headers on | off;
    # 显形/隐形 URI,上游发生重定向时,Nginx 是否同步更改 uri
    proxy_redirect on | off;
}

负载均衡配置

upstream,定义一个上游服务器集群

upstream backend {
    # ip_hash;
    server s1.barretlee.com;
    server s2.barretlee.com;
}
server {
    location / {
        proxy_pass http://backend;
    }
}

负载均衡策略

轮询	默认方式
weight	权重方式
ip_hash	依据ip分配方式
least_conn	最少连接方式
fair(第三方)	响应时间方式
url_hash(第三方)	依据URL分配方式
upstream dynamic_zuoyu {
    server localhost:8080  weight=2; #tomcat 7.0
    server localhost:8081; #tomcat 8.0
    server localhost:8082  backup; #tomcat 8.5
    server localhost:8083  max_fails=3 fail_timeout=20s; #tomcat 9.0
  }
  • weight参数用于指定轮询几率,weight的默认值为1,;weight的数值与访问比率成正比,比如Tomcat 7.0被访问的几率为其他服务器的两倍。

处理静态资源

# 如果请求路径跟文件路径按照如下方式匹配找到了,直接返回
try_files $uri $uri/index.html;
location ~* ^/(js|css|image|font)/$ {
    # 静态资源都在 static 文件夹下
    root /home/barret/www/static/;
}

try_files

server {
    root /www/nginx;
    index index.html index.php;
        
    charset utf-8;
    access_log /var/log/nginx/host.access.log  main;
    error_log /var/log/nginx/host.error.log  error;
        
    location ^~ /test/ {
        index index.html;

        #try_files /test1.html 1.html;
        try_files /2.html /1.html /test/test2.html;
    }
        
}

conf01


worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    
    # 监听80端口的服务
    server {
        listen       80;
        server_name  localhost;

        location / {
           root   /Users/ztc/www;
           index  index.html index.htm;
        }

    	# location /aa/ {
    	#     proxy_pass http://127.0.0.1:9000/;
    	#     proxy_redirect off;
        #         proxy_set_header Host $host:80;
        #         proxy_set_header X-Real-IP $remote_addr;
        #         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	# }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /Users/ztc/www$fastcgi_script_name;
	    include        fastcgi_params;
        }
    }
    
    # 监听8000端口的服务
    server {
       listen       8000;
       server_name  localhost;

       location / {
            root   /Users/ztc/www/test/;
            index  index.html index.htm;
       }
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

你可能感兴趣的:(Nginx)