nginx基础配置

来源:互联网 发布:阿里云dns 地址 编辑:程序博客网 时间:2024/05/21 18:45

1.图片服务器配置:

  1. location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js|html)$ {  
  2.                 expires 30d;  
  3.         }  
  4. #此目录的图片不缓存  
  5. location ^~ /picture/price/{  
  6. expires -2;  
  7. }  
  8. location ~*.(jsp|do|action|php|asp|rar|zip|txt|html|htm|shtml)$  
  9.         {  
  10.            deny all;  
  11.     }  

2.定制错误页面

http{

 fastcgi_intercept_errors on; 

}

 error_page 404  /404.html  

或者

 error_page 404 =http://www.xxx.com/404.html  

error_page  500 502 503 504  /50x.html; 

注意事项:
fastcgi_intercept_errors

语法:fastcgi_intercept_errors on|off 
默认值:fastcgi_intercept_errors off 
使用字段:http, server, location 
这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息

你必须明确的在error_page中指定处理方法使这个参数有效,正如Igor所说“如果没有适当的处理方法,nginx不会拦截一个错误,这个错误不会显示自己的默认页面,这里允许通过某些方法拦截错误。

3.nginx启动多个实例??

正常情况:

./configure --prefix=dir1
./configure --prefix=dir1 
能不能这样,用一个sbin/nginx多个配置文件,启动多个nginx实例,经测试,是可以的。但是重启时nginx -s stop/reload这两个实例都会重启。

4.nginx正则
^~     标识符后面跟一个字符串。Nginx将在这个字符串匹配后停止进行正则表达式的匹配(location指令中正则表达式的匹配的结果优先使用),如:location ^~ /images/,你希望对/images/这个目录进行一些特别的操作,如增加expires头,防盗链等,但是你又想把除了这个目录的图片外的所有图片只进行增加expires头的操作,这个操作可能会用到另外一个location,例如:location ~* \.(gif|jpg|jpeg)$,这样,如果有请求/images/1.jpg,nginx如何决定去进行哪个location中的操作呢?结果取决于标识符^~,如果你这样写:location /images/,这样nginx会将1.jpg匹配到location ~* \.(gif|jpg|jpeg)$这个location中,这并不是你需要的结果,而增加了^~这个标识符后,它在匹配了/images/这个字符串后就停止搜索其它带正则的location。

=      表示精确的查找地址,如location = /它只会匹配uri为/的请求,如果请求为/index.html,将查找另外的location,而不会匹配这个,当然可以写两个location,location = /和location /,这样/index.html将匹配到后者,如果你的站点对/的请求量较大,可以使用这个方法来加快请求的响应速度。

@      表示为一个location进行命名,即自定义一个location,这个location不能被外界所访问,只能用于Nginx产生的子请求,主要为error_page和try_files。
~      为区分大小写的匹配。
~*     不区分大小写的匹配(匹配firefox的正则同时匹配FireFox)。
!~     不匹配的
!~*    不匹配的

5.upsteam配置

worker_processes 1;  events {  worker_connections 1024;  }  http{  upstream myproject {  #这里指定多个源服务器,ip:端口,80端口的话可写可不写  server 192.168.43.158:80;  server 192.168.41.167;  }  server {  listen 8080;  location /two {  proxy_pass http://myproject;  }  }  }
注意在访问时,localhost/two 会报错,这样访问: localhost/two/(一定加反斜杠)
 

原创粉丝点击