nginx配置反向代理

摘要: 本文主要介绍利用nginx配置反向代理


假如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服务器了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值