2.3 ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
2.4 Redirect语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
2.5 防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
2.6 根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
2.7 禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
一些可用的全局变量:
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
2.8 Nginx静态文件(css,js,jpg等等web静态资源)
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
open_file_cache max=10000 inactive=60s;
location /group1/M00 {
root /data/fastdfs/data;
ngx_fastdfs_module;
}
location /css {
root plocc_static;
include gzip.conf;
}
location /common {
root plocc_static;
include gzip.conf;
}
2.9 nginx 转发工程的日志文件
去nginx.conf配置文件里面去看访问日志,如下:
vim nginx.conf
location ~* ^/mobileWeb/.*$ {
include deny.conf;
proxy_pass http://mobilewebbackend;
include proxy.conf;
error_log logs/mobileweb_error.log error;
access_log logs/mobileweb_access.log main;
include gzip.conf;
}
再去logs目录查看日志文件,如下:
[root@xx logs]# ll /usr/local/nginx/logs/mobileweb*
-rw-r--r--. 1 root root 10946 7月 18 10:36 /usr/local/nginx/logs/mobileweb_access.log
-rw-r--r--. 1 root root 1628 7月 18 10:36 /usr/local/nginx/logs/mobileweb_error.log
3 添加启动服务
- [root@localhost nginx]# cat /etc/init.d/nginx
- #!/bin/bash
- #chkconfig:2345 70 70
- #description:nginx
- BIN=/usr/nginx/sbin/nginx
- function d_start {
- $BIN || echo -n \"nginx is running\"
- }
- function d_stop {
- $BIN -s stop || echo -n \"nginx is not running\"
- }
- function d_reload {
- $BIN -s reload || echo -n \"nginx reload failed\"
- }
- case $1 in
- start)
- echo start nginx
- d_start
- ;;
- stop)
- echo stop nginx
- d_stop
- ;;
- reload)
- echo reload nginx
- d_reload
- ;;
- restart)
- echo restart nginx
- d_stop
- echo sleep 5s
- sleep 5
- d_start
- ;;
- *)
- echo \"Usage: nginx [start | stop |reload |restart]\"
- ;;
- esac
- exit 0
启动: service nginx start;
