nginx--反向代理配置(主要配置server)
2020-06-05 10:49
615 查看
案例1
访问http://localhost:8090时,重定向到http://localhost:8080
server { #监听本地8090端口 listen 8090; server_name localhost; location / { proxy_pass http://localhost:8080/; root html; index index.html index.htm; } # 重定向服务错误页面到静态页面50x.html # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
案例2
① 访问http://localhost:9001/dev时,重定向到http://localhost:8080
② 访问http://localhost:9001/dev/swagger-ui.html时,重定向到http://192.168.1.111:8081/swagger-ui.html
③ 访问http://localhost:9001/test/swagger-ui.html时,重定向到http://192.168.1.112:8081/swagger-ui.html
server{ #监听本地8090端口 listen 9001; server_name localhost; # =开头 表示精确匹配,只有完全匹配上才能生效 location =/dev { #被代理的url proxy_pass http://localhost:8080/; } # ^~开头 表示对URL路径进行前缀匹配,并且在正则之前 location ^~/dev { proxy_pass http://192.168.1.111:8081/; } location ^~/test { proxy_pass http://192.168.1.112:8081/; } #不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 location /pro{ proxy_pass http://192.168.1.113:8081/; } }
特别注意
当访问http://192.168.1.111:8081/dev/index.html时
①
代理到URL:http://127.0.0.1:8082/index.html
location /dev/ { #proxy_pass中最后以/结尾 proxy_pass http://127.0.0.1:8082/; }
②
代理到URL:http://127.0.0.1:8082/dev/index.html
location /dev/ { #proxy_pass中最后不是以/结尾 proxy_pass http://127.0.0.1:8082; }
③
代理到URL:http://127.0.0.1:8082/aaa/index.html
location /dev/ { proxy_pass http://127.0.0.1:8082/aaa/; }
④
代理到URL:http://127.0.0.1:8082/aaaindex.html
location /dev/ { proxy_pass http://127.0.0.1:8082/aaa; }
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,实际访问的路径中包含匹配的路径。
相关文章推荐
- nginx server配置和指令详解
- Debian/Ubuntu下Nginx的安装、反向代理和负载均衡的基本配置
- nginx反向代理配置及优化
- 利用Nginx做反向代理搭建ArcGIS 10.1 for Server集群环境
- nginx的proxy配置(反向代理)
- nginx配置:server_name的作用
- nginx 后端服务器配置端口 request.getserverPort
- 解决apache/nginx做反向代理导致request.getServerName()外网环境获取不到代理地址(或域名)的问题
- 解决apache/nginx做反向代理导致request.getServerName()外网环境获取不到代理地址(或域名)的问题
- Nginx配置httpsserver
- Nginx反向代理实现负载均衡配置图解
- 在ubuntu上面配置nginx实现反向代理
- nginx反向代理cas server之1:多个cas server负载均衡配置以及ssl配置
- Nginx反向代理以及配置优化
- Nginx之反向代理、日志格式、集群、缓存、压缩、URl 重写,读写分离配置 推荐
- nginx主要配置
- 有关nginx的配置文件 之server
- 二.Nginx反向代理和静态资源服务配置
- nginx配置出错 could not build the server_names_hash, you should increase either server_names_hash_max_si
- Nginx ServerName 配置说明(转)