0
点赞
收藏
分享

微信扫一扫

Nginx配置方向代理及目录白名单配置

何晓杰Dev 2022-02-21 阅读 176

目录

1、Nginx反向代理配置

2、目录白名单配置


1、Nginx反向代理配置

nginx 默认的配置文件是nginx.conf,进入nginx配置文件目录下打开配置文件

刚安装完的nginx.conf配置内容如下:

nginx 文件结构说明:

Nginx的核心配置文件主要由三个部分构成:
1)基本配置(全局模块)
2)events配置
3)http配置
        http{}模块包含server{}块和location{}块;而server{}块又包含location{}块。

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:
直接隶属于http{}块内的配置项称为main配置项
可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如连接超时时间,单连接请求数等。
4、server块:
直接隶属于server{}块内的配置项称为srv配置项
配置虚拟主机的相关参数,一个http中可以有多个server,可以有多个server,对应多个服务。
5、location块:
直接隶属于location{}块内的配置项称为loc配置项
配置请求的路由,以及各种页面的处理情况。

配置例子:

其他常用配置

配置1:

访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/login

配置2:

访问:http://127.0.0.1/test            转发到:http://10.23.2.96:8082/test
访问: http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/test/
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/test/login

配置3:

访问:http://127.0.0.1/test           转发到:http://10.23.2.96:8082/define
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/define/
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/define/login

配置4:

访问:http://127.0.0.1/test           转发到:http://192.168.2.96:8082/test/
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/test/
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/test/login

配置5:

访问:http://127.0.0.1/test           转发到:http://192.168.2.96:8082/test/
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/define
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/definelogin  #define与login之间不会有/ ,会直接拼接起来

配置6:

访问:http://127.0.0.1/test           转发到:http://10.23.2.96:8082/
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082//
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082//login

配置7:

访问:http://127.0.0.1/test           转发到:http://10.23.2.96:8082/define/
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/define//
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/define//login

配置8:

访问:http://127.0.0.1/test           转发到:http://127.0.0.1:8082/test/
访问:http://127.0.0.1/test/          转发到:http://10.23.2.96:8082/define/
访问:http://127.0.0.1/test/login   转发到:http://10.23.2.96:8082/define/login

备注:proxy_pass 后的URL如果符合 protocol://ip:port 同时结尾不加"/",则nginx会代理匹配路径部分,否则不代理匹配路径,同时自动添加不匹配路径"部分",比如/testone/login的/login部分

2、目录白名单配置

配置1:校验一级目录

nginx.conf配置如下:

对应目录配置如下:

 访问:http://xxx.xxx.xx.xx:8443/reader/open 无法转发到:http://21.13.245.185:8090/reader/open

访问:http://xxx.xxx.xx.xx:8443/reader           无法转发到:http://21.13.245.185:8090/reader

 访问:http://xxx.xxx.xx.xx:8443/minio/open   转发到:http://21.13.245.185:8090/minio/open

访问:http://xxx.xxx.xx.xx:8443/minio             转发到:http://21.13.245.185:8090/minio

配置1:校验二级目录

nginx.conf配置如下:

对应目录配置如下:

访问:http://127.0.0.1:8080/minio/test              无法转发到:http://wly.test.com:8090/wps_result/open/minio/test
访问:http://127.0.0.1:8080/minio/test/aa          无法转发到:http://wly.test.com:8090/wps_result/open/minio/test/aa
访问:http://127.0.0.1:8080/minio/adapter          转发到:http://wly.test.com:8090/wps_result/open/minio/adapter
访问:http://127.0.0.1:8080/minio/adapter/aa      转发到:http://wly.test.com:8090/wps_result/open/minio/adapter/aa

举报

相关推荐

0 条评论