Nginx 常见配置解析

结构上:

核心模块:HTTP模块、EVENT模块、MAIL模块。
基础模块:HTTP access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块。
第三方模块:HTTP Upstream Request Hash模块。

功能上:

Handlers:处理请求,进行输出内容和修改headers信息等。
Filters:主要对其他处理模块输出的内容进行修改操作。
Proxies:Nginx的HTTP Upstream之类的模块,与后端服务如fastcgi等进行交互。

工作模式:

单工作进程
多工作进程

Nginx与Apache最大区别:

Nginx的模块直接编译进Nginx,属于静态编译方式,启动Nginx自动加载。
Apache将模块编译成一个so文件,需要在配置文件中指定是否加载。

静态文件处理:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        root /web/wwwroot/;
        expires 30d;       -----指定静态文件过期时间
    }

配置获取nginx的运行状态:

location /NginxStatus {
        stub_status    on;    ---启用StubStatus的工作状态统计功能
        access_log    logs/NginxStatus.log;    ---指定访问日志文件
        auth_basic    "NginxStatus";    ---Nginx的一种认证机制
        auth_basic_user_file    ../htpasswd;    ---指定认证密码文件
    }
# /usr/local/apache/bin/htpasswd -c /usr/local/nginx/conf/htpasswd webadmin

Nginx信息:
Active connection: 1      ----当前活跃的链接数
server accepts handled reqests       
 393411  393411  393799      ---总共处理了393411个连接,创建393411次握手,处理393799个请求
reading: 0  Writiong: 1 Waiting: 0

Nginx启动、关闭重启

QUIT:处理完当前请求后关闭进程。
HUP:重新加载配置没平滑重启Nginx。
USR1:Nginx日志切换,重亲打开一个日志文件。
USR2:平滑升级执行。
WINCH:从容关闭工作进程。

nginx反向代理根问题测试:

访问192.168.8.104:  192.168.8.103目录test下有index.html测试用

location / {
        proxy_pass http://192.168.8.103/test;
    }
出现301

location / {
        proxy_pass http://192.168.8.103/test/;
    }   
正常访问

访问192.168.8.104/test/test.html:

location /test {
        proxy_pass http://192.168.8.103/app;
    }
访问到/app/test.html

location /test/ {
        proxy_pass http://192.168.8.103/app;
    }
访问到/apptest.html

location /test {
        proxy_pass http://192.168.8.103/app/;
    }
 访问到/app//test.html

location /test/ {
        proxy_pass http://192.168.8.103/app/;
    }
访问到/app/test.html

location /test {
        proxy_pass http://192.168.8.103;
    }
访问到/test/test.html

location /test {
        proxy_pass http://192.168.8.103/;
    }
访问到//test.html

location /test/ {
        proxy_pass http://192.168.8.103;
    }
访问到/test/test.html

location /test/ {
        proxy_pass http://192.168.8.103/;
    }
访问到/test.html

总结 如果location路径是根,那么proxy_pass 路径必须加根。
     如果proxy_pass 没有路径, location路径会被完整的传递下来。
     如果proxy_pass有路径,location路径排除匹配所用的部分会完整的传递下来。

别名:

location /i {
    alias /var/www/html/imags/;    
}
url请求 /i/logo.gif 服务器会查找/var/www/html/imags/logo.gif
如果是root  会查找/var/www/thml/imags/i/logo.gif

location ~ ^/download/(.*)$ {
    alias /home/webdata/www/$1;
}
url请求 /download/ebook.tar.gz ,Nginx服务器会请求 /home/webdata/www/ebook.tar.gz 文件
如果是root 会查找/home/webdata/www/ebook.tar.gz/download/ebook.tar.gz

 

rewrite:

语法: rewrite regex flag
默认:none
使用字段:server 、 location 、 if
flag种类:
last:rewrite之后搜索相应的URI或location
break:终止匹配,不在匹配后面规则
redirect:返回302,游览器显式跳转后的地址
permanent:返回301,游览器显式跳转后的地址

重定向实现域名过度:
server {
    server_name  com;
    if ($host != ' {
        rewrite ^/(.*)$  permanent; 
    }   

}

缓存:

proxy_cache_path /backup/proxy_cache_dir
levels=1:2
keys_zone=cache_one:4096m
inactive=1d 
max_size=3g;
proxy_temp_path /backup/proxy_temp_dir;  需跟proxy_cache_path分在一个磁盘
location中配置
proxy_cache cache_one;
proxy_cache_valid 200 304 12h
proxy_cache_key $host$url$is_args$args;

清楚缓存:

location ~ /purge(/.*)
{
    allow 127.0.0.1;
    allow 192.168.88.0/24
    deny all;
    proxy_cache_purge cache_one $host$1$is_args$args
}
 的缓存可以通过访问
 清除

原创文章,作者:心肝坏了,如若转载,请注明出处:http://www.178linux.com/47953

赞 (0)
心肝坏了心肝坏了
上一篇 2016-12-05 17:03
下一篇 2016-12-05 17:03

相关推荐

  • select与case组合循环

    select与case: 格式: select variable in list do 循环体命令 done 例1:     #!/bin/bash PS3="what do you want: " select i in a b c d         #在例2中省略…

    Linux干货 2016-08-22
  • linux学习第二天知识点-linux入门及使用帮助

    一 基础命令 1. #ifconfig     显示或配置网卡的命令,英文全称是network interfaces configuring。     配置网卡的IP地址语法例:ifconfig eth0 192.168.0.1 netmask 255.255.255.0 2.&…

    Linux干货 2016-07-26
  • Memcached命令

    Memcache 的命令行用法: 1、启动Memcache 常用参数    memcached 1.4.3      -p <num>      设置端口号(默认不设置为: 11211)      -U <num> &nbsp…

    Linux干货 2015-05-18
  • Linux 基础知识(二)

    一、Linux文件管理类命令的使用方法及其相关示例演示? 1、文件管理 2、文件查看 二、Bash特性详解 1、命令行展开 (1)在/tmp目录下创建:a_c,a_d,b_c,b_d 方法一:mkdir -pv {a,b}_{c,d} (2)在/tmp目录下创建mylinux目录,其结构如下:         方法一:…

    Linux干货 2016-09-26
  • nginx基础模块

    目录: nginx基础模块: 1.ngx_http_access_module模块: 2.ngx_http_auth_basic_module模块 3.ngx_http_stub_status_module模块 4.ngx_http_log_module模块 5.ngx_http_gzip_module: 6.ngx_http_ssl_module模块: 7…

    Linux干货 2017-08-08
  • lvm 逻辑卷管理

    标签:LVM 创建 扩容 缩减 一、LVM  LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管…

    Linux干货 2015-05-18