Nginx 代理转发

Nginx 代理转发

1. nginx 转发请求到 tomcat, 实现负载均衡

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    upstream tomcatCluster{
        server 127.0.0.1:8081 weight=1;
        server 127.0.0.1:8082 weight=1;
        server ip:8080 weight=1;//其它服务器
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://tomcatCluster;

            check interval=3000 rise=2 fall=3 timeout=3000 type=http;
        	check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        	check_http_expect_alive http_2xx http_3xx http_4xx;

         }

        error_page 404 /404.html;
            location = /40x.html {
        }

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

}

2. nginx 域名重定向

将a.host.com 重定向到 b.host.com

server {
    listen       80 ;
    server_name a.host.com;
    root   /usr/share/nginx/html/a.host.com;
	
	location / {
        proxy_pass http://b.host.com;
        proxy_set_header X-Real-IP $remote_addr;
    }
}






你可能感兴趣的:(Nginx)