nginx小实验(双端口代理不同的网站)

nginx小实验(双端口代理不同的网站)

环境

OS
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.7.1908 (Core)
Release: 7.7.1908
Codename: Core

安装

yum上有nginx,用yum install -y nginx.x86_x64安装即可

配置

场景

新增两个端口,访问55544端口时反向代理到百度,访问55545端口时访问新浪

准备

nginx安装目录为

/usr/local/nginx

步骤

  • nginx.conf中http配置块中加入
include /usr/local/nginx/conf/custom/*.conf;
  • /usr/local/nginx/conf/custom/ 目录下分别加入baidu.conf和sina.conf
  • baidu.conf内容如下
upstream baidu {
    server www.baidu.com;
}
server {
    listen       55544;

    location / {
		proxy_set_header Host www.baidu.com;
        proxy_pass http://baidu;
    }
}

或者

server {
    listen       55544;
    location / {
		proxy_set_header Host www.baidu.com;
        proxy_pass http://www.baidu.com;
    }
}
  • sina.conf内容如下
upstream sina {
    server www.sina.com.cn;
}
server {
    listen       55545;

    location / {
		proxy_set_header Host www.sina.com.cn;
        proxy_pass http://sina;
    }
}

或者

server {
    listen       55545;
    location / {
		proxy_set_header Host www.sina.com.cn;
        proxy_pass http://www.sina.com.cn;
    }
}
  • 用nigix -t -c /usr/local/nginx/conf/custom/*.conf 测试配置文件
  • 无误后用nginx -c /usr/local/nginx/conf/custom/*.conf 启动nginx
  • End.

相关命令

启动

nginx
启动nginx
nginx -c <文件路径>
以某个文件为配置文件启动nginx
nginx -t
测试配置文件语法
nginx -t -c <文件路径>
测试某个配置文件的语法

停止

nginx -s quit
温和退出nginx
nginx -s stop
强制停止nginx
killall nginx
强制停止nginx进程

其他

nginx -s reload
重新载入修改后的配置文件

参考

  1. nginx高级玩法之根据来源ip分流
  2. Nginx(三)------nginx 反向代理
  3. nginx config的多个config配置
  4. nginx查看日志
  5. Nginx 配置详解
  6. Nginx 服务器安装及配置文件详解
  7. Nginx 反向代理与负载均衡详解
  8. Nginx 安装配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值