nginx配置url重定向-反向代理
来源:互联网 发布:闪电网络何时上线 编辑:程序博客网 时间:2024/06/14 10:39
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://lansgg.blog.51cto.com/5675165/1575274
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
正则表达式匹配,其中:
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -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永久重定向。
1
2
3
rewrite ^(
/download/
.*)
/media/
(.*)\..*$ $1
/mp3/
$2.mp3 last;
rewrite ^(
/download/
.*)
/audio/
(.*)\..*$ $1
/mp3/
$2.ra last;
return
403;
1
2
3
4
5
location
/download/
{
rewrite ^(
/download/
.*)
/media/
(.*)\..*$ $1
/mp3/
$2.mp3
break
;
rewrite ^(
/download/
.*)
/audio/
(.*)\..*$ $1
/mp3/
$2.ra
break
;
return
403;
}
1
rewrite ^
/users/
(.*)$
/show
?user=$1? last;
1
/photos/123456
1
/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;
1
2
3
4
server {
server_name www.example.com;
rewrite ^ http:
//example
.com$request_uri? permanent;
}
1
2
3
if
($args ^~ post=100){
rewrite ^ http:
//example
.com
/new-address
.html? permanent;
}
1
2
3
4
5
6
7
8
9
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/;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
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 区域
1
2
3
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
log_not_found off;
#不记录404 not found 错误日志 expires 30d;
break
; }
1
2
3
4
location ~ /(resource|mediatorModule)/ {
root
/opt/demo
;
expires max;
}
1
2
3
4
5
6
location ^~
/html/scripts/loadhead_1
.js {
access_log off;
root
/opt/lampp/htdocs/web
;
expires 600;
break
;
}
1
2
3
4
gzip
on;
gzip_min_length 1000;
gzip_buffers 48k;
gzip_types text
/plain
application
/x-javascript
text
/css
text
/html
application
/xml
;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
1
2
3
4
5
6
7
8
9
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}
}
1
2
mkdir
/var/www/html/other
echo
"192.168.10.129"
>
/var/www/html/other/index
.html
1
2
[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"
0 0
- nginx配置url重定向-反向代理
- nginx的重定向与反向代理
- Nginx配置解读,虚拟主机,https配置,反向代理,https重定向
- nginx proxy反向代理兼内部重定向
- nginx 反向代理,负载均衡,重定向,读写分离
- nginx 反向代理,负载均衡,重定向,读写分离
- nginx反向代理配置
- nginx反向代理配置
- nginx反向代理配置
- Nginx 配置反向代理
- nginx反向代理配置
- nginx反向代理配置
- Nginx反向代理配置
- nginx配置反向代理
- nginx反向代理配置
- Nginx反向代理配置
- nginx反向代理配置
- Nginx反向代理配置
- Linux网络编程入门
- 同步与异步的概念
- html5标签
- IOS:Ready for sale之后的一些事儿,关于Availability Date
- 安卓应用程序插件化开发框架 -AAP Framework
- nginx配置url重定向-反向代理
- 爬楼梯
- redhat常用命令
- Android Studio 的快捷键
- 拓扑排序
- Winform开发框架之插件化应用框架实现
- linux(debian)固定IP的设置
- 【总结】手机图片预览插件photoswipe使用总结
- Android 自定义view(二) 如何实现自定义组件