nginx配置反向代理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

假如nginx安装后的目录为: /usr/local/nginx.

则首先在conf目录下,创建vhost文件夹:

mkdir /usr/local/nginx/conf/vhost

然后在vhost文件夹内,创建负载均衡配置文件upstream.server:

touch /usr/local/nginx/conf/vhost/upstream.server

然后往upstream.server写入负载均衡信息:

upstream testServer {
  server 192.168.1.2;
  server 192.168.1.3:8080;
  server Ip地址+端口;
  server 或者域名地址;
}

upstream 自定义服务器名称 {
  server ip地址+端口;
  server 多个则加多行;
}

然后再分别创建各个服务器路径代理关系配置文件:

touch /usr/local/nginx/conf/vhost/test.nginx.conf

touch /usr/local/nginx/conf/vhost/自定义服务器名称.nginx.conf

往该配置文件(test.nginx.conf)写入路径代理信息:

location /test/path1/ {
  proxy_pass http://testServer/path1/;
}

location /test/path2/ {
  proxy_pass http://testServer/path2/;
}
location /自定义路径/xx/ {
  proxy_pass http://自定义服务器名称/需代理路径/;
}

 

然后修改nginx.conf配置文件:

user root root;
worker_processes  auto;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  10240;
}

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

    include /usr/local/nginx/conf/vhost/upstream.server;

    server {
        listen       80;
        server_name  localhost;
        underscores_in_headers on;
        

        location / {
            root   html;
            index  index.html index.htm;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        include /usr/local/nginx/conf/vhost/*.conf;
    }
}

注: 以上配置文件只保留了关键点,主要增加

include /usr/local/nginx/conf/vhost/upstream.server;
include /usr/local/nginx/conf/vhost/*.conf;

这两个配置。

至此,整个代理配置就配置完成了。

假设 nginx 所在机器Ip=192.168.1.110,开放80端口:

那么通过以上配置,就能通过 http://192.168.1.110/test/ 访问 test服务器了。

 

转载于:https://my.oschina.net/mobinchao/blog/1605425

你可能感兴趣的:(nginx配置反向代理)