Nginx反向代理绑定多个域名
安装nginx依赖包
安装gcc yum -y install gcc-c++ 安装PCRE正则表达式解析 yum install -y pcre pcre-devel 安装zlib解压缩 yum install -y zlib zlib-devel 安装openssl yum install -y openssl openssl-devel
安装nginx
1.命令行中依次运行以下命令
wget http://nginx.org/download/nginx-1.17.5.tar.gz tar -zxvf nginx-1.17.5.tar.gz cd nginx-1.17.5 ./configure make make install
2.新建临时缓存文件夹,在/var下创建temp/nginx,即/var/temp/nginx
3.进入/usr/local/nginx查看文件是否存在conf、sbin、html文件夹,若存在则安装成功
4.添加进用户可以执行文件
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
5. 运行 nginx 命令启动nginx服务
nginx
6.测试nginx服务是否运行成功
1.运行 curl localhost 进行测试启动,会出现以下结果
<title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
2.输入公网ip访问也行,或者ps aux|grep nginx查看进程,会出现master和worker
配置nginx.conf
1.创建配置文件并编辑
在conf文件夹里面新建文件夹 vhosts ,并分别建立配置文件 baidu.com.conf,根据自己的域名创建 为什么不直接编辑nginx.conf文件,因为主配置文件nginx.conf中指定包含其他扩展配置文件,从而简化nginx主配置文件, 实现多个站点功能 配置文件中加入代码 server { listen 80; server_name baidu.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_pass http://127.0.0.1:8080; } } server_name 代表您需要绑定的域名 proxy_pass 代表服务器上运行程序的地址(带端口号) listen 代表 nginx 监听端口,默认80
2.在nginx.conf引入配置文件
在 conf 文件夹下找到 nginx.conf 文件 在http配置项中加入配置:
include vhosts/*.conf;
其他指令
nginx -s stop //关闭nginx
nginx -s reload //重启nginx