nginx配置url重定向-反向代理
正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配,其中: * -f和!-f用来判断是否存在文件 * -d和!-d用来判断是否存在目录 * -e和!-e用来判断是否存在文件或目录 * -x和!-x用来判断文件是否可执行 flag标记有: * last 相当于Apache里的[L]标记,表示完成rewrite * break 终止匹配, 不再匹配后面的规则 * redirect 返回302临时重定向 地址栏会显示跳转后的地址 * permanent 返回301永久重定向 地址栏会显示跳转后的地址 一些可用的全局变量有,可以用做条件判断 $args, 请求中的参数; $content_length, HTTP请求信息里的"Content-Length"; $content_type, 请求信息里的"Content-Type"; $document_root, 针对当前请求的根路径设置值; $document_uri, 与$uri相同; $host, 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; $limit_rate, 对连接速率的限制; $request_method, 请求的方法,比如"GET"、"POST"等; $remote_addr, 客户端地址; $remote_port, 客户端端口号; $remote_user, 客户端用户名,认证用; $request_filename, 当前请求的文件路径名 $request_body_file $request_uri, 请求的URI,带查询字符串; $query_string, 与$args相同; $scheme, 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect; $server_protocol, 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr, 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); $server_name, 请求到达的服务器名; $server_port, 请求到达的服务器端口号; $uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。
last - 完成重写指令,之后搜索相应的URI或location。
break - 完成重写指令。
redirect - 返回302临时重定向,如果替换字段用http://开头则被使用。
permanent - 返回301永久重定向。
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403;
location /download/ { rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403; }
rewrite ^/users/(.*)$ /show?user=$1? last;
/photos/123456
/path/to/photos/12/1234/123456.png
1 | rewrite "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/ $1/$1$2/$1$2$3.png; |
server { server_name www.example.com; rewrite ^ http://example.com$request_uri? permanent; }
if ($args ^~ post=100){ rewrite ^ http://example.com/new-address.html? permanent; }
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; rewrite ^/ http://www.Aries.com/; }
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; location /c1.html { rewrite /c1.html /c2.html break; } location /c2.html { return 508; } } [root@master sbin]# echo "c1" > /opt/nginx/nginx/html/lansgg/c1.html [root@master sbin]# echo "c2" > /opt/nginx/nginx/html/lansgg/c2.html
使用break会停止匹配下面的location,直接发起请求www.lansgg.com/c1.html,他会显示c2的内容;
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; location /c1.html { rewrite /c1.html /c2.html last; } location /c2.html { return 508; } }
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; if ($http_host = www.lansgg.com){ rewrite (.*) http://www.Aries.com; } }
uninitialized_variable_warn 指令 可使用 http, server, location, if 区域
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ { log_not_found off; #不记录404 not found 错误日志 expires 30d; break; }
location ~ /(resource|mediatorModule)/ { root /opt/demo; expires max; }
location ^~ /html/scripts/loadhead_1.js { access_log off; root /opt/lampp/htdocs/web; expires 600; break; }
gzip on; gzip_min_length 1000; gzip_buffers 48k; gzip_types text/plain application/x-javascript text/css text/html application/xml;
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; rewrite ^/c/(.*)$ http://www.lansgg.com/cc/$1; } [root@master lansgg]# tree . ├── c │ └── index.html ├── cc │ └── index.html ├── index.html └── it.jpg 2 directories, 4 files
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; index index.html; rewrite ^/c/(.*)$ /cc/$1; }
server { listen 80 default_server; server_name www.lansgg.com lansgg.com; access_log logs/lansgg.access.log main; error_log logs/lansgg.error.log; root /opt/nginx/nginx/html/lansgg; location / { index index.html; } location /other { proxy_pass http://192.168.10.129/other; proxy_set_header X-Real-IP $remote_addr; } }
2.3、配置client1
mkdir /var/www/html/other echo "192.168.10.129" > /var/www/html/other/index.html
[root@client1 ~]# tail -f /var/log/httpd/access_log 192.168.10.1 - - [06/Nov/2014:21:25:44 +0800] "GET /other/ HTTP/1.1" 200 15 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
码字很辛苦,转载请注明来自朱一兵的博客的《nginx配置url重定向-反向代理》
2016-04-26
学习文章
评论
| Theme by 暴博客 基于Z-BlogPHP搭建
文章归档
最近发表
标签