前提:已经在虚拟机中配置好nginx(docker),有可访问的项目
1.找到host文件所在位置:
C:\Windows\System32\drivers\etc
进行修改,加入你想要的域名和你的虚拟机地址,比如:
xs.com 192.168.56.10
接着访问xs.com:9200(这里可以是随便一个服务,比如nacos),正常访问既是设置成功
或者直接访问xs.com,出现了nginx相关信息即代表设置成功
2.进入挂载在外的nginx文件,
其中conf.d中的default.conf是conf文件夹下的nginx.conf的子文件,无论有什么内容都会包含进去。
[root@localhost ~]# cd /mydata/nginx/conf/conf.d/
[root@localhost conf.d]# cat default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3.复制一份新的配置文件
[root@localhost conf.d]# cp default.conf xs.conf
[root@localhost conf.d]# vi xs.conf
修改以下内容:
server_name xs.com;
然后回到windows使用ipconig命令得到虚拟机的ipv4地址,比如我的就是192.168.56.1
回到xs.conf,修改得到以下内容:
location / {
proxy_pass http://192.168.56.1:9001;
}
其中9001是你的项目模块的端口
保存退出,重启nginx
[root@localhost conf.d]# docker restart nginx
windows访问xs.com,转到项目设置的默认页即算成功
4.配置网关负载均衡
重新回到上级的nginx.conf,在“include /etc/nginx/conf.d/*.conf;”上面配置上游服务:
[root@localhost conf.d]# cd ../
[root@localhost conf]# vi nginx.conf
upstream xs{
server 192.168.56.1:88;
}
我使用88端口是因为我的项目网关配置的是88端口。
再回到xs.conf
[root@localhost conf]# cd conf.d
[root@localhost conf.d]# vi xs.conf
修改得到以下内容:
location / {
proxy_set_header Host $host;
proxy_pass http://xs;
}
保存退出。
5.回到项目网关进行配置,我的是springcloud gateway。
在applicaiton.yml中加入以下配置:
- id: xs_host_route uri: lb://xs predicates: - Host=**.xs.com
其中xs就是你要负载均衡的模块的名字
再次访问xs.com,转到项目设置的默认页即算成功