如果您对nginxproxy_pass指令感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于nginxproxy_pass指令的详细内容,我们还将为您解答nginxproxy-
如果您对nginx proxy_pass 指令感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于nginx proxy_pass 指令的详细内容,我们还将为您解答nginx proxy-pass的相关问题,并且为您提供关于3-nginx 之 proxy_pass详解、docker - nginx - proxy_pass + proxy_redirect、Nginx + Naxsi 配置(proxy_pass)、nginx proxy_cache配置说明 nginx proxy cache nginx tcp proxy nginx proxy pass配的有价值信息。
本文目录一览:- nginx proxy_pass 指令(nginx proxy-pass)
- 3-nginx 之 proxy_pass详解
- docker - nginx - proxy_pass + proxy_redirect
- Nginx + Naxsi 配置(proxy_pass)
- nginx proxy_cache配置说明 nginx proxy cache nginx tcp proxy nginx proxy pass配
nginx proxy_pass 指令(nginx proxy-pass)
nginx proxy_pass 指令
文档
Nginx 官方文档
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Nginx 服务器的反向代理 proxy_pass 配置方法讲解
https://www.cnblogs.com/lianxuan1768/p/8383804.html
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a protocol, “http” or “https” can be specified. The address can be specified as a domain name or IP address, and an optional port:
A request URI is passed to the server as follows:
# 如果 proxy_pass 指令指定了 URI ,则传给服务器的请求的 URI 中匹配 location 指令的部分将被去除掉。
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
# 如果 proxy_pass 指令没有指定 URI ,则传给服务器的请求的 URI 被原样传递。
If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
测试应用的目录结构
[root@localhost ~]# tree ROOT
ROOT
├── proxy
│ ├── mozq
│ │ └── test.html
│ ├── mozqtest.html
│ └── test.html
└── test.html
请求的 url 都是 www.mozq.com/proxy/test.html
所有请求的 uri 是 /proxy/test.html
upstream tomcatserver {
server 172.17.0.3:8080;
}
server {
listen 80;
server_name www.mozq.com;
location /proxy/ {
proxy_pass http://tomcatserver/;
index index.html index.htm;
}
}
#请求
www.mozq.com/proxy/test.html
# 实际访问
/test.html
server {
listen 80;
server_name www.mozq.com;
location /proxy/ {
proxy_pass http://tomcatserver/mozq/;
index index.html index.htm;
}
}
#请求
www.mozq.com/proxy/test.html
# 实际访问
/mozq/test.html
server {
listen 80;
server_name www.mozq.com;
location /proxy/ {
proxy_pass http://tomcatserver;
index index.html index.htm;
}
}
#请求
www.mozq.com/proxy/test.html
# 实际访问
/proxy/test.html
server {
listen 80;
server_name www.mozq.com;
location /proxy/ {
proxy_pass http://tomcatserver/mozq;
index index.html index.htm;
}
}
#请求
www.mozq.com/proxy/test.html
# 实际访问
/mozqtest.html
总结
请求: www.mozq.com/proxy/test.html
请求的 URI 为: /proxy/test.html
location: /proxy/
proxy_pass: 带 URI 的情况。请求的 URI 中匹配 location 指定的部分被去除掉。请求的 URI 去除掉被 location 指令匹配的部分后为 test.html 。拼接。
http://tomcatserver/ 结果: http://tomcatserver/test.html
http://tomcatserver/mozq 结果: http://tomcatserver/mozqtest.html
http://tomcatserver/mozq/ 结果: http://tomcatserver/mozq/test.html
proxy_pass: 不带 URI 的情况。使用原请求的 URI 。拼接。
http://tomcatserver 结果: http://tomcatserver/proxy/test.html
3-nginx 之 proxy_pass详解
参考:https://www.jianshu.com/p/b010c9302cd0
在Nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。
第一种:
location /proxy/ {
proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html
第二种(相对于第一种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html
第三种:
location /proxy/ {
proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html
第四种(相对于第三种,最后少一个 / )
location /proxy/ {
proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
Nginx中有两个模块都有proxy_pass
指令。
-
ngx_http_proxy_module
的proxy_pass
:
语法: proxy_pass URL;场景: location, if in location, limit_except说明: 设置后端代理服务器的协议(protocol)和地址(address),以及location中可以匹配的一个可选的URI。协议可以是"http"或"https"。地址可以是一个域名或ip地址和端口,或者一个 unix-domain socket 路径。 详见官方文档: http://Nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_passURI的匹配,本文第四部分重点讨论。
ngx_stream_proxy_module
的proxy_pass
:
语法: proxy_pass address;场景: server说明: 设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。 详见官方文档: http://Nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass
二、两个proxy_pass
的关系和区别
在两个模块中,两个proxy_pass
都是用来做后端代理的指令。
ngx_stream_proxy_module
模块的proxy_pass
指令只能在server段使用使用, 只需要提供域名或ip地址和端口。可以理解为端口转发,可以是tcp端口,也可以是udp端口。
ngx_http_proxy_module
模块的proxy_pass
指令需要在location段,location中的if段,limit_except段中使用,处理需要提供域名或ip地址和端口外,还需要提供协议,如"http"或"https",还有一个可选的uri可以配置。
三、proxy_pass的具体用法
ngx_stream_proxy_module
模块的proxy_pass
指令
server {
listen 127.0.0.1:12345;
proxy_pass 127.0.0.1:8080;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 1m;
proxy_pass example.com:12345;
}
server {
listen 53 udp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass dns.example.com:53;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
ngx_http_proxy_module
模块的proxy_pass
指令
server {
listen 80;
server_name www.test.com;
# 正常代理,不修改后端url的
location /some/path/ {
proxy_pass http://127.0.0.1;
}
# 修改后端URL地址的代理(本例后端地址中,最后带了一个斜线)
location /testb {
proxy_pass http://www.other.com:8801/;
}
# 使用 if in location
location /google {
if ( $geoip_country_code ~ (RU|CN) ) {
proxy_pass http://www.google.hk;
}
}
location /yongfu/ {
# 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/
proxy_pass http://unix:/tmp/backend.socket:/uri/;;
# 匹配到请求方法为: PUT or DELETE, 代理到9080
limit_except PUT DELETE {
proxy_pass http://127.0.0.1:9080;
}
}
}
四、proxy_pass
后,后端服务器的url
(request_uri
)情况分析
server {
listen 80;
server_name www.test.com;
# 情形A
# 访问 http://www.test.com/testa/aaaa
# 后端的request_uri为: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 访问 http://www.test.com/testb/bbbb
# 后端的request_uri为: /bbbb
location ^~ /testb/ {
proxy_pass http://127.0.0.1:8801/;
}
# 情形C
# 下面这段location是正确的
location ~ /testc {
proxy_pass http://127.0.0.1:8801;
}
# 情形D
# 下面这段location是错误的
#
# Nginx -t 时,会报如下错误:
#
# Nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
# expression, or inside named location, or inside "if" statement, or inside
# "limit_except" block in /opt/app/Nginx/conf/vhost/test.conf:17
#
# 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!!
}
# 情形E
# 访问 http://www.test.com/ccc/bbbb
# 后端的request_uri为: /aaa/ccc/bbbb
location /ccc/ {
proxy_pass http://127.0.0.1:8801/aaa$request_uri;
}
# 情形F
# 访问 http://www.test.com/namea/ddd
# 后端的request_uri为: /yongfu?namea=ddd
location /namea/ {
rewrite /namea/([^/]+) /yongfu?namea=$1 break;
proxy_pass http://127.0.0.1:8801;
}
# 情形G
# 访问 http://www.test.com/nameb/eee
# 后端的request_uri为: /yongfu?nameb=eee
location /nameb/ {
rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
proxy_pass http://127.0.0.1:8801/;
}
access_log /data/logs/www/www.test.com.log;
}
server {
listen 8801;
server_name www.test.com;
root /data/www/test;
index index.PHP index.html;
rewrite ^(.*)$ /test.PHP?u=$1 last;
location ~ \.PHP$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.PHP;
include fastcgi.conf;
}
access_log /data/logs/www/www.test.com.8801.log;
}
文件: /data/www/test/test.PHP
<?PHP
echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];
通过查看 $_SERVER['REQUEST_URI'] 的值,我们可以看到每次请求的后端的request_uri的值,进行验证。
小结
情形A和情形B进行对比,可以知道proxy_pass
后带一个URI,可以是斜杠(/)也可以是其他uri,对后端request_uri
变量的影响。
情形D说明,当location为正则表达式时,proxy_pass
不能包含URI部分。
情形E通过变量($request_uri, 也可以是其他变量),对后端的request_uri
进行改写。
情形F和情形G通过rewrite配合break标志,对url进行改写,并改写后端的request_uri
。需要注意,proxy_pass
地址的URI部分在情形G中无效,不管如何设置,都会被忽略。
docker - nginx - proxy_pass + proxy_redirect
目的:使项目域名 www.foo.test(/index.php)/controller/action
变为 www.foo.test/project/controller/action
,仍然可以正常访问。(非index.php二级目录的URL重写)
背景:
Name Command State Ports
------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1 dockerd-entrypoint.sh Up 2375/tcp
laradock_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
laradock_nginx_1 nginx Up 0.0.0.0:443->443/tcp,
0.0.0.0:80->80/tcp
laradock_php-fpm_1 docker-php-entrypoint php-fpm Up 9000/tcp
laradock_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp
foo.conf
配置为
server {
listen 80;
listen [::]:80;
server_name www.foo.test;
root /var/www/foo/web;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
#try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
#fastcgi_buffers 16 16k;
#fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
#fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
error_log /var/log/nginx/laravel_error.log;
access_log /var/log/nginx/laravel_access.log;
}
解决:
在 foo.conf
配置里添加
location /project {
proxy_pass http://127.0.0.1/;
proxy_redirect http://127.0.0.1/ /project;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
访问 www.foo.test/project/controller/action
,成功。
总结:
一开始,同事给我发了一份测试服务器上的配置,
location /project {
proxy_pass http://127.0.0.1:9000/;
proxy_redirect http://127.0.0.1:9000/ /project;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
可我按这个配置写入我本的 nginx
的配置时,死活不行。后还想到,我自己用的docker
环境(测试环境上不是),那么 127.0.0.1
根本不是我的 php-fpm
容器地址。如果要连接,需要用到php-fpm
的容器名称。
所以实际上,nginx
容器本地地址 127.0.0.1:80
其实就是 php-fpm
的 9000
端口。
最后,删除了 proxy_pass http://127.0.0.1:9000/;
中的 9000
后配置成功了。
Nginx + Naxsi 配置(proxy_pass)
如何解决Nginx + Naxsi 配置(proxy_pass)?
我正在尝试使用 Naxsi 作为代理配置 Nginx(将请求重定向到另一个域)。问题是,通过这种方式,Naxsi 不起作用(如果我使用基本的 Nginx 来做,没有重定向,配置工作正常)。你知道这是否可以做到吗?
Nginx.conf
user www-data;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include /etc/Nginx/naxsi_core.rules;
include /etc/Nginx/conf.d/*.conf;
include /etc/Nginx/sites-enabled/*;
default_type application/octet-stream;
access_log /var/log/Nginx/access.log;
error_log /var/log/Nginx/error.log;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name example.com;
location / {
include /etc/Nginx/naxsi.rules;
proxy_set_header ''Access-Control-Allow-Origin'' ''http://midominio.com'';
proxy_set_header ''Access-Control-Allow-Methods'' ''GET,POST,OPTIONS,PUT,DELETE'';
proxy_set_header ''Access-Control-Allow-Headers'' ''X-Requested-With,Accept,Content-Type,Origin'';
proxy_pass http://otherexample.es;
proxy_redirect off;
proxy_buffering on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header origin ''http://example.com'';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
naxi.rules
SecrulesEnabled;
DeniedUrl "/error.html";
## Check for all the rules
CheckRule "$sql >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
nginx proxy_cache配置说明 nginx proxy cache nginx tcp proxy nginx proxy pass配
#定义代理缓存路径 ,缓存文件保存路径 缓存初始大小和最大值 缓存时间
proxy_cache_path d:\code\cache levels=1:2 keys_z max_size=20m inactive=1m;
location / {
#配置上面定义的keys_zone的值
proxy_cache cache1;
#配置url中包括哪个参数的时候不去缓存中查询
proxy_cache_bypass $arg_name;
#定义缓存的key
proxy_cache_key $host$uri$is_args$args;
#缓存时间,如果response中header信息里边有Cache-Control 则以header里边的缓存时间为准
proxy_cache_valid 2m;
#当缓存失效后回源出错的时候可以使用缓存中旧数据,旧的总比出错强
proxy_cache_use_stale error timeout http_502 http_404;
proxy_pass http://127.0.0.1:8080;
}
以上就介绍了
今天关于nginx proxy_pass 指令和nginx proxy-pass的介绍到此结束,谢谢您的阅读,有关3-nginx 之 proxy_pass详解、docker - nginx - proxy_pass + proxy_redirect、Nginx + Naxsi 配置(proxy_pass)、nginx proxy_cache配置说明 nginx proxy cache nginx tcp proxy nginx proxy pass配等更多相关知识的信息可以在本站进行查询。
本文标签: