环境
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
重新载入修改后的配置文件