手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Nginx安装配置使用详细笔记

[日期:2014-07-22] 来源:Linux社区  作者:mchdba [字体: ]

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$&quot {
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 添加启动服务

  1. [root@localhost nginx]# cat /etc/init.d/nginx
  2. #!/bin/bash
  3. #chkconfig:2345 70 70
  4. #description:nginx
  5. BIN=/usr/nginx/sbin/nginx
  6. function d_start {
  7. $BIN || echo -n \"nginx is running\"
  8. }
  9. function d_stop {
  10. $BIN -s stop || echo -n \"nginx is not running\"
  11. }
  12. function d_reload {
  13. $BIN -s reload || echo -n \"nginx reload failed\"
  14. }
  15. case $1 in
  16. start)
  17. echo start nginx
  18. d_start
  19. ;;
  20. stop)
  21. echo stop nginx
  22. d_stop
  23. ;;
  24. reload)
  25. echo reload nginx
  26. d_reload
  27. ;;
  28. restart)
  29. echo restart nginx
  30. d_stop
  31. echo sleep 5s
  32. sleep 5
  33. d_start
  34. ;;
  35. *)
  36. echo \"Usage: nginx [start | stop |reload |restart]\"
  37. ;;
  38. esac
  39. exit 0

启动: service nginx start;

linux
相关资讯       Nginx安装  Nginx安装配置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款