反向代理配置成功
首先,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/
反向代理的配置规则:
其中的每个斜杆必不可少,否则会匹配不到。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |