虚拟机centos安装nginx并配置

1,虚拟机我使用的是wm ware,安装的centos 7 64位

2.准备安装文件,使用winscp将文件移动到虚拟机/home/leyou目录下

3.解压,并删除多余的压缩包

tar -xvf nginx-1.10.0.tar.gz

rm -rf nginx-1.10.0.tar.gz

4.配置安装目录

 ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx

5.编译安装,安装完后可以在/opt/nginx下看到

make && make install

6.关闭防火墙或者关闭80端口的防火墙,我这里直接把防火墙关了,

systemctl stop firewalld.service 

同时要关闭防火墙开机自启动

systemctl disable firewalld.service

如果一直访问不到可以先开启,再关闭

7.启动nginx,并查看状态,如图所示则成功

nginx

ps -ef | grep nginx

在主机访问虚拟机结果如下:

6.进入/opt/nginx/conf打开并编辑配置文件nginx.conf,在相同的server后面添加下面代码,配置监控ip,转发ip

server {
        listen 80;
        server_name manage.leyou.com;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        location / {
            proxy_pass http://127.0.0.1:9001;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }
    server {
        listen 80;
        server_name api.leyou.com;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        location / {
            proxy_pass 192.168.227.182:10010;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }

注意:(根据自己需求配置)

server_name 监控的访问地址 即manage.leyou.com;

location / { //这个为转发的地址和端口
            proxy_pass 192.168.227.182:9001;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }

8.重新加载服务,本机如图所示即配置生效(这里之所以是502是因为我192.168.227.182:9001配置的问题,修改好就可以正常访问了)

nginx -s reload

9.修改switchhost,并开启

 10.在本机ping一下manage.leyou.com,如图所示即成功了,ping通虚拟机即switch host生效了

猜你喜欢

转载自blog.csdn.net/qq_36603180/article/details/120902995
今日推荐