- 如果请求的文件不存在,则反向代理到localhost 。这里的break也是停止继续rewrite
if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
- 对/images/bla_500x400.jpg文件请求,重写到/resizer/bla.jpg?width=500&height=400地址,并会继续尝试匹配location。
rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
- rewrite语法格式
- IF判断和内置全局环境变量
proxy_pass
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
- 不影响浏览器地址栏的url
- 设置被代理server的协议和地址,URI可选(可以有,也可以没有)
- 协议可以为http或https
- 地址可以为域名或者IP,端口可选;eg:
proxy_pass http://localhost:8000/uri/;
- 如果一个域名可以解析到多个地址,那么这些地址会被轮流使用,此外,还可以把一个地址指定为 server group(如:nginx的upstream), eg:
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
server {
location / {
proxy_pass http://backend;
}
- server name, port, URI支持变量的形式,eg:
proxy_pass http://$host$uri;
这种情况下,nginx会在server groups(upstream后端server)里搜索server name,如果没有找到,会用dns解析
请求的URI按照下面的规则传给后端server
- 如果proxy_pass的URL定向里包括URI,那么请求中匹配到location中URI的部分会被proxy_pass后面URL中的URI替换,eg:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
请求http://127.0.0.1/name/test.html 会被代理到http://example.com/remote/test.html
- 如果proxy_pass的URL定向里不包括URI,那么请求中的URI会保持原样传送给后端server,eg:
location /name/ {
proxy_pass http://127.0.0.1;
请求http://127.0.0.1/name/test.html 会被代理到http://127.0.0.1/name/test.html
- 一些情况下,不能确定替换的URI
- location里是正则表达式,这种情况下,proxy_pass里最好不要有URI
- 在proxy_pass前面用了rewrite成功,如下,这种情况下,proxy_pass中的URL是无效的,eg:
location /name/ {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1/useless; #/useless无效
}
疑惑? 往下看。
4.如果proxy_pass指令指定了URI,那么请求中匹配location部分的URI将会被替换:
location /name {
proxy_pass http://127.0.0.1:8080/other;
#http://exampe.org/name/a.jhtml将会被转发给:
#http://127.0.0.1:8080/other/a.jhtml
5.如果location中使用了uri,但是proxy_pass中没有使用,那么请求的uri将会全部添加到proxy_pass指定的路径之后。
location /name {
proxy_pass http://127.0.0.1:8080;
#请求url为http://example.org/name/a.jhtml将会被转发到
#http://127.0.0.1:8080/name/a.jhtml
6.如果location中使用了正则表达式,那么在proxy_pass指令中不应该指定uri,否则uri的替换是无法断定的。
7.如果在location中使用了“rewrite”指令(break)对请求的uri进行了修改,那么proxy_pass指令中的uri将会被忽略,被rewrite之后的全量uri将会传递给server。
location /name {
rewrite /name/([^/]+) /users?name=$1 break;
proxy_pass http://127.0.0.1/useless;
#http://exmaple.org/name/zhangsan, 将会转发到 #http://127.0.0.1/users?name=zhangsan
8.其中server名称、端口、uri等均可以使用变量:
proxy_pass http://$host$uri;
##等价于
proxy_pass $request;
需求信息如下:
- html、js、css、jpg等静态资源尽量走nginx;
- 首页默认不带index.html 但是也走nginx;
- 前后端分离+优化url(笔者不确定这种优化是否起反作用),/s/?wd=xxx --> list.html?wd=xxx /details/{num} --> details.html?u={num}
- 其余请求转发到8000端口jetty容器(java应用);
- itblacklist.cn的请求及权重永久转到www.itblacklist.cn;
- tucao.itblacklist.cn转到php应用;
直接上配置
server {
server_name www.itblacklist.cn;
listen 80;
root /opt/apps/blacklist/page; #nginx server根目录
# =精准匹配 /首页 该规则只匹配首页
location =/ {}
# ~* 不区分大小写的正则匹配 匹配所有以.html .gif等结尾的请求
location ~* \.(html|gif|jpg|ico|js|css|map|svg|eot|tff|woff|woff2)$ {}
# ^~ 匹配以某个开头的请求 该处匹配以/s/开头的请求
location ^~ /s/ {
rewrite /s/(.+)$ /$1 break;
proxy_pass http://www.itblacklist.cn/list.html;
#... proxy头信息等
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
location ~* /details/([0-9]+)$ {
rewrite /details/(.+)$ /details.html?u=$1 break;
location / {
proxy_pass http://www.itblacklist.cn:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
server {
server_name itblacklist.cn;
rewrite ^(.*)$ http://wwww.itblacklist.cn$1 permanent;
server {
server_name tucao.itblacklist.cn;
location / {
proxy_pass http://www.itblacklist.cn:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
重新加载nginx配置: nginx -s reload
location /Shep.ElicenseWeb/ {
rewrite ^/Shep.ElicenseWeb/(.*) /$1 break;
proxy_pass http://localhost:82;
}
http://192.168.64.76/Shep.ElicenseWeb/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
Should be redirected to:
http://localhost:82/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
3.1 1location without regular expression
-
If the proxy_pass
directive is specified without a URI,
location /app/ {
proxy_pass http://192.168.154.102;
test.com/app/xxxxx => http://192.168.154.102/xxxxx
-
If the proxy_pass
directive is specified with a URI:
location /app/ {
proxy_pass http://192.168.154.102/maped_dir/;
test.com/app/xxxxx => http://192.168.154.102/maped_dir/xxxxx
-
Forward the requested Host header
By default, the Host header from the request is not forwarded, but is set based on the proxy_pass statement. To forward the requested Host header, it is necessary to use:
proxy_set_header Host $host;
3.2. location with regular expression
-
If the location is given by regular expression, can not be a URI part in proxy_pass
directive, unless there are variables in the directive.
location ~ ^/app/(.*)$ {
# proxy_pass http://127.0.0.1/some_dir; # error
proxy_pass http://127.0.0.1/some_dir/$1; # ok
variables in proxy_pass
directive:
location ~ ^/app/(.*)$ {
proxy_pass http://192.169.154.102:$1;
test.com/app/8081 => http://192.168.154.102:8081
and:
location ~ ^/app/(.*)$ {
proxy_pass http://192.169.154.102:9999/some_dir/$1;
test.com/app/request/xxxxx => http://192.168.154.102:9999/some_dir/xxxxx
with a rewrite directive in the location:
If the rewrite rule is hit, the URI specified in the directive is ignored and the full changed request URI is passed to the server:如果rewrite匹配到了,则使用新的URL传送给proxy_pass
location /app/ {
rewrite ^/app/hit/(.*)$ /hit_page.php?path=$1 break;
proxy_pass http://192.168.154.102:9999/some_dir/;
}
/app/hit/some/request/?name=xxxxx rewrite匹配成功:
=> http://192.168.154.102:9999/hit_page.php?path=some/request/&name=xxxxx
/app/not_hit/some/request/?name=xxxxx rewrite匹配失败:
=> http://192.168.154.102:9999/some_dir/not_hit/some/request/?name=xxxxx
https://jonathanmh.com/proxy-rewrite-api-endpoint-domain-segment-nginx/
https://www.liaohuqiu.net/posts/nginx-proxy-pass/
https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite
https://superuser.com/questions/690916/how-to-make-nginx-rewrite-uris-in-http-body-content
https://yq.aliyun.com/articles/9095
https://blog.csdn.net/mdjhzgj/article/details/78442422
https://www.cnblogs.com/luxianghao/p/6807081.html
https://www.jianshu.com/p/10ecc107b5ee
最近接触到了nginx的 proxy_pass 与 rewrite,在这里记录一下,学习学习。说明rewritesyntax: rewrite regex replacement [flag]Default: —Context: server, location, if如果正则表达式(regex)匹配到了请求的URI(request URI),这个URI会被后面的replac...
proxy_pass http://www.xxx.com/;
proxy_pass http://192.168.200.101:8080/uri;
proxy_pass unix:/tmp/www.sock;
二 proxy_pass的注意案例
案例描述:
假设 nginx服务器的域名为:www.xxx.com
后端服务器为:192.168.1.10
当请求http://www.xxx.com/aming/a.
目录nginx proxy_pass部分加/和不加/的区别nginx proxy_pass部分加/和不加/的区别
最近在是使用nginx配置代理,正好顺便看到其他博主的 博客,认为比较好,所以粘贴记录一下,以免后面使用的时候找不见:
proxy_pass末尾有斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/;
请求地...
代理(proxy),即中间人,它代替客户端发送请求给服务器,收到响应后再转给客户端。通常意义上的代理是从用户的角度讲的,用户通过某个代理可以访问多个网站,这个代理是靠近用户的,比如某些公司可能需要限制员工所访问的网站,就会在网络出口处放置一个代理来做过滤。
反向代理(reverse proxy),本质上跟代理是一回事,只不过是从服务器的角度讲的,是靠近服务器的。比如某个网站有多个服务器,提供同样的功能,一般会在网络入口处放一个代理,接收客户端的请求,再基于某种策略(比如轮转)转发给后端
最初是 用户 --> http 1081 --> proxy_pass 14.xx.xx.xx:10810
现在需要改成: 用户 --> http 1081 --> https 1083 --> proxy_pass 14.xx.xx.xx:10810 (网闸地址)
原本nginx配置:
server{
listen 1081;
index index.shtml index.php index.htm member.html
(1)重写url,可以重写path,也可以重写整个url(存在协议,默认返回302,代表临时跳转)。
(2)4 个 flag:last、break、redirect、permanent。存在 flag 时,在当前location 中,不再执行之后的 rewrite指令集(包括 rewrite、return)中的指令;break 和 last 正好相反,break 中止对其它location 的规则匹配,last 继续向其它 location 进行规则匹配;
2、使用举例...
前端地址是:http://baidu.com,后端地址是:http://taobao.com
那么前端如果直接写死后端地址的话,一是要处理跨域问题,二是如果后端换地址需要改前端代码
最好的办法就是通过反向代理,比如http://baidu.com/api然他指向http://taobao.com,把**/api**替换掉就好了
像vue有vue.config.js中配置反向代理来重写路径,但实
upstream datacollectbackend{
#ip_hash;
server 10.234.1.211:6100 max_fails=5 fail_timeout=30s;
server 10.234.1.26:7100 max_fails=5 fail_timeout=30s;
locat...
rewrite概述
现在 Nginx 已经成为很多公司作为前端反向代理(proxy_pass)服务器的首选,在实际工作中往往会遇到很多跳转(重写 URL)的需求。
比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。如果在后端使用的 Apache 服务器,虽然也能做跳转,规则库也很强大,但是用 Nginx 跳转效率会更高(正则精确匹配)
www.kgc.com/ 每个星期周四的上午10:00-11:00 会进行维护,在此期间来访问的用户,..
在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
第二种(相对于第一种,最后少一个 / )
locati
可以一起使用,rewrite用于重写URL,proxy_pass用于将请求代理到指定的后端服务器。在nginx配置中,可以使用rewrite来修改URL,然后使用proxy_pass将请求代理到修改后的URL对应的后端服务器。例如:
location / {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://backend_server;
这个配置将会将所有以/api/开头的请求重写为不包含/api/的URL,并将请求代理到backend_server服务器。