nginx配置

1 修改nginx配置后http://www.cnblogs.com/derekchen/archive/2011/02/17/1957209.html

/usr/nginx/sbin/nginx -s reload

重启nginx才能生效。注意,必须测试(见下一条)无误后才能生效

2 nginx及配置  http://blog.csdn.net/fangaoxin/article/details/7045330

测试某个配置文件是否书写正确 命令

sbin/nginx – t – c conf/nginx2.conf 

具体Nginx 的参数包括:

-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
-v:显示 nginx 版本号。
-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。

 

3 Nginx配置文件 conf/nginx.conf
先来看一个实际的配置文件:

user nobody;# 工作进程的属主
worker_processes 1;                 # 工作进程数,一般与 CPU 核数等同

#error_log logs/error.log; 
#error_log logs/error.log notice; 
#error_log logs/error.log info; 
#pid        logs/nginx.pid; 

events { 
    use epoll;                             #Linux 下性能最好的 event 模式
    worker_connections 2048;     # 每个工作进程允许最大的同时连接数

http { 
    include       mime.types; 
    default_type application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] $request ' 
    #                  '"$status" $body_bytes_sent "$http_referer" ' 
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log off; 
    access_log logs/access.log;          # 日志文件名

    sendfile        on;                         
    #tcp_nopush     on; 
    tcp_nodelay     on;

    keepalive_timeout 65;

    include gzip.conf; 
    
    # 集群中的所有后台服务器的配置信息 简单的负载平衡,单服务器不用配置
    upstream tomcats { 
           server 192.168.0.11:8080 weight=10; 
           server 192.168.0.11:8081 weight=10; 
           server 192.168.0.12:8080 weight=10; 
           server 192.168.0.12:8081 weight=10; 
           server 192.168.0.13:8080 weight=10; 
           server 192.168.0.13:8081 weight=10; 
    }

    server { 
        listen 80;                       #HTTP 的端口
        server_name localhost;   #服务器名称,可以多域名支持

        charset utf-8;

        #access_log logs/host.access.log main;

        location ~ ^/NginxStatus/ { 
            stub_status on; #Nginx 状态监控配置
            access_log off; 
        }

        location ~ ^/(WEB-INF)/ { 
            deny all; 
        }

  

    #正则配置
        location ~ .(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf)$ { 
             root /opt/webapp;     #访问的目录
             expires 24h;             #客户端的过期时间
        }

        location / { 
             proxy_pass http://tomcats;  # 反向代理 刚才设置的,Nginx通过访问内部的服务器获得相应的服务。
             include proxy.conf; 
        }

        error_page 404 /html/404.html;

        # redirect server error pages to the static page /50x.html 
        # 
        error_page 502 503 /html/502.html; 
        error_page 500 504 /50x.html; 
        location = /50x.html { 
            root html; 
        } 
    } 
}

 

4 server可以单独配置,用include包含在 conf/nginx.conf 中

server {
  listen 80;
  server_name localhost;
  index index.html index.htm index.php;
  root /alidata/www/default;
  location ~ .*.(php|php5)?$
  {
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;  # 反向代理,9000是php的端口
    fastcgi_index index.php;
    include fastcgi.conf;
  }
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  }
  location ~ .*.(js|css)?$
  {
    expires 1h;
  }
  #伪静态规则
  include /alidata/server/nginx/conf/rewrite/default.conf;
  access_log /alidata/log/nginx/access/default.log;
}

5 伪静态rewrite

  # 前面是正则匹配,$1,$2表示子匹配式
  rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
  rewrite ^(.*)/simple/([a-z0-9\_]+.html)$ $1/simple/index.php?$2 last;
  rewrite ^(.*)/data/(.*).(htm|php)$ 404.html last;
  rewrite ^(.*)/attachment/(.*).(htm|php)$ 404.html last;
  rewrite ^(.*)/html/(.*).(htm|php)$ 404.html last;

Location语法 http://www.t086.com/article/4619

  1、~   为区分大小写匹配
  2、~* 为不区分大小写匹配
  3、!~和!~* 不匹配

  示例一:
    location  /  {}   #匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。
  示例二:
    location =/ {}   #仅仅匹配/  注意和一的区别
  示例三:                   #图像文件
    location ~* .(gif|jpg|jpeg)$ {
      rewrite .(gif|jpg)$ /logo.png;

    }

7 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: ginx/html
  $request_filename:D: ginx/html/test1/test2/test.php

  3 示例
      server {
        listen 80;
        server_name start.igrow.cn;
        index index.html index.php;
        root html;


        if ($http_host !~ "^www.itlearner.com$ {
             rewrite ^(.*) http://www.itlearner.com$1 redirect;
        }
      }
  

  4 防盗链
    location ~* .(gif|jpg|swf)$ {
      valid_referers none blocked start.igrow.cn sta.igrow.cn;
      if ($invalid_referer) {
        rewrite ^/ http://$host/logo.png;
      }
    }

  5根据文件类型设置过期时间
    location ~* .(js|css|jpg|jpeg|gif|png|swf)$ {
      if (-f $request_filename) {
        expires    1h;
        break;
      }
    }
  6 禁止访问某个目录
    location ~* .(txt|doc)${ 
          root /data/www/wwwroot/linuxtone/test;
        deny all;
    }

原文地址:https://www.cnblogs.com/mitang/p/3953551.html