Windows环境下Nginx配置本地虚拟域名和Nginx代理

Windows环境下Nginx配置本地虚拟域名和Nginx代理

一、准备

(1) 进入 Nginx 安装目录,如:E:\GreenSofts\Nginx-1.21.4

(2) 检查当前目录下是否存在 etc\init.d,如:E:\GreenSofts\Nginx-1.21.4\etc\init.d;若不存在,则新建目录

(3) 在 nginx.confhttp 配置段内添加:

#Load configuration files for the default server block.
include E:/GreenSofts/Nginx-1.21.4/etc/init.d/*.conf;

用来读取 /etc/init.d 下的虚拟主机配置,如:

在这里插入图片描述

二、配置

(1) 进入 /etc/init.d 目录,新建一个 Nexus.conf (你自己想取的名字)文件,如:

server {
    listen       80;
    server_name  michael.nexus.com;

    location / {
		proxy_pass http://127.0.0.1:8888/;
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
    }
}

(2) 配置 server_name 为自己指定的虚拟域名,如:michael.nexus.com

(3) 配置本机的DNS

修改本地 hosts 文件(C:\Windows\System32\drivers\etc\hosts),添加 127.0.0.1 michael.nexus.com ,如:

在这里插入图片描述

(4) 重启 nginx

(5) 然后打开浏览器访问 michael.nexus.com 测试

三、通过nginx去代理端口号

如:E:\GreenSofts\Nginx-1.21.4\etc\init.d

Nexus.conf

upstream michael.nexus.com {
	server 127.0.0.1:8888;
}

server {
	listen       80;
	server_name  michael.nexus.com;

	location / {
		proxy_pass http://michael.nexus.com;
		proxy_set_header Host $http_host;
		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; 这一行的作用是把原http请求的Header中的Host字段也放到转发的请求里

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值