1. Create nginxdocker container
docker run -d -p 80:80 --name=mynginx nginx
2. Prepare the nginx configuration file
① In the virtual machine, create an empty directory
mkdir /opt/nginxconf
② Copy the nginx configuration file to an empty directory
cp /usr/local/nginx/conf/nginx.conf /opt/nginxconf
③ Modify the nginx configuration file
Note: The reverse proxy is the ip of the project started by idea
server {
listen 80;
server_name localhost;
# 获取客户端ip设置到请求头中 反向代理后 微服务获取的真实ip就是浏览器ip
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ /core/ {
proxy_pass http://192.168.1.1:8160;
proxy_connect_timeout 20;
}
location ~ /sms/ {
proxy_pass http://192.168.1.1:8120;
proxy_connect_timeout 20;
}
}
3. Copy the nginx configuration to the container to replace the default
docker cp /opt/nginxconf/nginx.conf mynginx:/etc/nginx/
4. Restart the nginx container
docker restart mynginx
5. Test
Access nginx test reverse proxy in browser
http://虚拟机ip:80/admin/core/dict/parent/1