结构上:
核心模块: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

