nginx部分功能配置备忘
-
跨域配置(POST方式)
add_header 'Access-Control-Allow-Headers' 'Content-Type';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'POST'; -
静态文件访问
location /login {
alias /data/nginx_data/html/hebei/admin/;
index index.html ;
break ;
} -
文件上传
upstream xxxxx{
server 127.0.0.1:10013;
}
location =/api/xxx/user/headimg {
client_max_body_size 3M;
proxy_pass http://xxxxx/user/headimg;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header api_version $http_api_version ;
proxy_set_header api_version $http_apiversion ;
proxy_connect_timeout 20s; #连接超时 默认为60秒
proxy_read_timeout 20s; #读取超时 默认为60秒
proxy_send_timeout 20s; #发送超时 默认为60秒
break;
} -
路径重定向
location /zc {
rewrite '.*' 'http://www.baidu.com';
}location /static/upload/ {
rewrite ^/static/upload/(.*)$ http://www.xxx.com/api/static/upload/$1 redirect;
} -
文件服务器
upstream file.static {
server 127.0.0.1:38088 ;
}
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=1g;
location /api/static_file/ {
proxy_pass http://file.static/;
proxy_method GET;
access_log off;
expires 30d;
proxy_cache imgcache;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 10m;
proxy_cache_valid any 1h;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
break ;
}
server{
listen 38088;
charset UTF-8 ;
location / {
default_type 'application/json;charset=UTF-8';
alias /data/nginx_data/html/static/;
break;
}
} -
svn代码服务器
location / {
proxy_pass http://svn/ ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header Host $http_host ;client_max_body_size 1024M;
proxy_connect_timeout 9999s; #连接超时 默认为60秒
proxy_read_timeout 9999s; #读取超时 默认为60秒
proxy_send_timeout 9999s; #发送超时 默认为60秒break;
} -
root和alias区别(原文链接)
nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域:
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:locationroot与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~root实例:
location ^~ /t/ {
root /www/root/html/;
}如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
alias实例:
location ^~ /t/ {
alias /www/root/html/new_t/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。
注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。注意:
(1)使用alias时,目录名后面一定要加"/"。
(2)alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
(3)alias只能位于location块中。(root可以不放在location中)nginx是通过alias设置虚拟目录,在nginx的配置中,alias目录和root目录是有区别的:
1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
3)alias虚拟目录配置中,location匹配的path目录如果后面不带"/",那么访问的url地址中这个path目录后面
加不加"/"不影响访问,访问时它会自动加上"/";但是如果location匹配的path目录后面加上"/",
那么访问的url地址中这个path目录必须要加上"/",访问时它不会自动加上"/"。如果不加上"/",访问就会失败!
4)root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。举例说明(比如nginx配置的域名是www.wangshibo.com):
示例一
location /huan/ {
alias /home/www/huan/;
}在上面alias虚拟目录配置下,访问http://www.wangshibo.com/huan/a.html实际指定的是/home/www/huan/a.html。
注意:alias指定的目录后面必须要加上"/",即/home/www/huan/不能改成/home/www/huan上面的配置也可以改成root目录配置,如下,这样nginx就会去/home/www/huan下寻找http://www.wangshibo.com/huan的访问资源,两者配置后的访问效果是一样的!
location /huan/ {
root /home/www/;
}

浙公网安备 33010602011771号