nginx 配置多服务器代理

找到 nginx > conf目录中nginx.conf 

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
     '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # access_log  /var/log/nginx/access.log  main;

    # 负载均衡演示之 upstream 参考 https://blog.csdn.net/caijunsen/article/details/83002219
    upstream psjcserver {
	    server 192.168.20.81:8091;
	    #server 172.18.253.44:8083;
    }

    # 读取conf.d目录的conf配置
    include "E:/nginx-1.17.2/conf/conf.d/*.conf";


}

  

在nginx > conf目录中创建一个conf.d目录 (conf.d用于存放多个服务器配置)

目录中创建了一个admin.conf,用于在下图中被识别*conf

  

生产的admin.conf代码,管理admin服务器

server {
	listen       80;
	server_name  127.0.0.1;

	location / {
		root   "E:/admin";
		index  index.html index.htm;
	}

	location /admin/ {
	    # rewrite 匹配 localhost/admin 转成 http://psjcserver
	    rewrite  ^.+admin/?(.*)$ /$1 break;
	    # proxy_pass 设置代理借口
		proxy_pass http://psjcserver;
		# 如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败,报400错误
		proxy_set_header Host $http_host;
		$autoindex  on;
	}

	location /favicon.ico {
		root	html;
	}

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

  

猜你喜欢

转载自www.cnblogs.com/hpx2020/p/11239922.html