配置nginx反向代理,实现api.x.com域名代理本地9001端口

1、安装niginx,启动服务。

[root@nginx ~]#yum install -y nginx
[root@nginx ~]#systemctl enable --now nginx

2、创建nginx配置文件,写入反向代理信息。

[root@nginx ~]#vim /etc/nginx/conf.d/nginx.conf
server {
        listen 80;
        server_name api.x.com;
        location / {
            proxy_pass http://127.0.0.1:9001/;
        }
   }

server {
        listen 9001;
        server_name 127.0.0.1;
        location / {
            root /data/api.x.com;
            index index.html;
        }
   }

3、创建9001端口的html测试页面。

[root@nginx ~]#mkdir -p /data/api.x.com
[root@nginx ~]#echo 9001 > /data/api.x.com/index.html

4、修改hosts文件,解析api.x.com域名。

[root@nginx ~]#echo "10.0.0.58 api.x.com" >> /etc/hosts

5、重新启动nginx服务。

[root@nginx ~]#systemctl restart nginx

6、成功访问api.x.com。

[root@nginx ~]#curl api.x.com
9001
posted @ 2021-02-01 10:18  IT乐乐  阅读(487)  评论(1)    收藏  举报