1、配置本机的dns
- 进入C:\Windows\System32\drivers\etc\hosts进行修改(此为示例域名,需改为自己需要的)
127.0.0.1 localhost user.gmall.com cart.gmall.com manage.gmall.com www.gmall.com …
- 然后打开cmd,输入ipconfig /flushdns进行刷新
- 此时可以直接访问http://user.gmall.com:8080/index,相当于访问了http://localhost:8080/index(访问index是因为我的controller的代码)
- controller代码如下
@GetMapping("index")
public String index(){
return "hello user";
}
2、通过nginx去代理端口号
- 在nginx文件夹找到conf文件夹并进入,打开nginx.conf,在http内加入如下代码
upstream user.gmall.com{
server 127.0.0.1:8080;
}
server {
listen 80;
server_name user.gmall.com;
location / {
proxy_pass http://user.gmall.com;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
}
}
- 然后启动nginx即可通过http://user.gmall.com/index访问
- 最后注意一点,这个域名只有自己可以访问,别人访问不了,真正的是需要买的,这个就是模拟一下。