Nginx
十一、Nginx Redirect、Location
#### Nginx Redirect - 将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到http://www.linuxtone.org ```shell server { listen 80; server_name linuxtone.org netseek.linuxtone.org; index index.html index.php; root /data/www/wwwroot; if ($host !~ "^www.linxtone.org$") { rewrite ^(.*) http://www.linuxtone.org$1 redirect; } ..................... } ``` #### Nginx Location - 基本语法:[和上面rewrite正则匹配语法基本一致] location [=|~|~*|^~] /uri/ { … } ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配 - 示例1: ```shell location = / { # matches the query / only. # 只匹配 / 查询。 } ``` 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配 - 示例2: ```shell location ^~ /images/ { # matches any query beginning with /images/ and halts searching, # so regular expressions will not be checked. # 匹配任何以 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。 } ``` - 示例3: ```shell location ~* .(gif|jpg|jpeg)$ { # matches any request ending in gif, jpg, or jpeg. However, all # requests to the /images/ directory will be handled by # 匹配任何以 gif、jpg 或 jpeg 结尾的请求。 } ``` #### Nginx expires - 1.根据文件类型判断,添加expires ```shell # Add expires header for static content location ~* .(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { root /data/www/wwwroot/bbs; expires 1d; break; } } ``` - 2、根据某个目录判断,添加expires ```shell # serve static files location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /data/www/wwwroot/down; expires 30d; } ``` #### Nginx 目录自动加斜线: ```shell if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } ``` #### Nginx 日志处理 - 1.Nginx 日志切割 ```shell #contab -e 59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1 # cat /usr/local/sbin/logcron.sh #!/bin/bash log_dir="/data/logs" time=`date +%Y%m%d` /bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.logkill -USR1 `cat /var/run/nginx.pid` ``` - 2.利用AWSTATS分析NGINX日志 设置好Nginx日志格式,仍后利用awstats进行分析. - 3.Nginx 如何不记录部分日志 日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了 ```shell location ~ .*.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ { access_log off; } ```
顶部
收展
底部
[TOC]
目录
一、 Nginx 基础知识
二、 Nginx 安装及调试
三、nginx php-fpm安装配置
四、常见错误处理
五、Nginx监控
六、Nginx 负载均衡
七、nginx反向代理配置
八、Nginx Rewrite
九、Nginx安全
十、nginx基本配置与参数说明
十一、Nginx Redirect、Location
十二、nginx 高并发配置参数
十三、Nginx 参数优化
十四、Nginx 虚拟主机配置例子
相关推荐
Apache