Nginx 反向代理的配置和注意点(成功配置)
反向代理配置成功
首先,Nginx 和 Java 后端都运行在云服务器的 docker 容器中。ps: 需要确保云服务器端口正常开放,以及两个容器都能被正常的访问。
现在想让 ng 做反向代理达到如下目的:通过前端 url 地址的映射,来访问后端的接口。
反向代理流程:前端 url 地址 =》ng服务器 =》 后端服务器。
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 82; # 监听的端口 server_name localhost; # 域名或ip location / { # 访问路径配置 root /usr/share/nginx/html/regist/;# 根目录 index index.html; # 默认首页 } # 配置如上,通过 localhost:82 的方式就可以访问到 index.html # 如下是配置反向代理,浏览器通过访问 http://云服务器ip:82/reg/ # 就可以访问到后端http://云服务器ip:8800/ location /reg/ { proxy_pass http://云服务器ip:8800/; } error_page 500 502 503 504 /50x.html; # 错误页面 location = /50x.html { root html; } } log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; 111 include /etc/nginx/conf.d/*.conf; }
注意
反向代理的配置规则:
反向代理的映射:
http://ip:82/reg/ 对应到 http://云服务器ip:8800/
反向代理的配置规则:
其中的每个斜杆必不可少,否则会匹配不到。
本文作者:曲琦
本文链接:https://www.cnblogs.com/123525-m/p/17459600.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步