Docker Nginx 前后端配置代理
docker nginx挂载相关命令(以微信机器人项目为例)
docker run -d -p 80:80 -p 443:443 --name nginx -v /usr/nginx/www:/usr/share/nginx/html -v /usr/nginx/nginx.conf:/etc/nginx/nginx.conf -v /usr/nginx/logs:/var/log/nginx nginx
前端文件放在 /usr/nginx/www/roobot/ 目录下 目录结构如下
相关docker nginx 代理配置文件如下
upstream roobot_server {
#docker默认的网桥地址172.17.0.1 通过该地址访问localhost 9201为后端项目的端口 server 172.17.0.1:9201; keepalive 2000; } server {
#监听80端口,前端访问时 访问地址为ip:80/接口 listen 80; server_name *****; location / {
#代理的实际路径为设置的对应路径 即172.17.0.0:9201/ proxy_pass http://roobot_server/; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 300s; proxy_send_timeout 500s; proxy_read_timeout 500s; }
#配置访问页面的代理 当访问http://ip/roobot/时跳转到部署的前端页面即上述第一步所示目录中的index.html location /roobot/ { root /usr/share/nginx/html; index index.html index.htm; }
#代理目录下的静态资源文件 location ~.*(js|css|png|gif|jpg|mp3|ogg|ttf|woff)$ { root /usr/share/nginx/html/roobot; } }
如果有配置ssl则如下 (未尝试,由其他项目配置修改)
server { listen 80; server_name 域名; rewrite ^(.*) https://$host$1 permanent; } server { listen 443 ssl; server_name 域名;
#证书记得复制到docker nginx的目录下 ssl_certificate 证书.pem; ssl_certificate_key 证书.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; location / { proxy_pass http://172.17.0.1:9201; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 300s; proxy_send_timeout 500s; proxy_read_timeout 500s; }
location /roobot/ {
root /usr/share/nginx/html; index index.html index.htm; }
location ~.*(js|css|png|gif|jpg|mp3|ogg|ttf|woff)$ { root /usr/share/nginx/html/roobot; }
}