[关闭]
@52fhy 2015-12-17T17:41:02.000000Z 字数 3471 阅读 614

理解nginx的配置

Nginx


Nginx配置文件主要分成四部分:main(全局设置)server(主机设置)upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)location(URL匹配特定位置后的设置),每部分包含若干个指令。

main部分设置的指令将影响其它所有部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(比如,根目录"/","/images",等等)。

他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。

可以有多个server,location属于server子集。

一个完整的nginx.conf

  1. user www www;
  2. worker_processes 1; # worker角色的工作进程个数,简单可设置CPU核数
  3. error_log /www/log/nginx/error.log crit;
  4. pid /www/server/nginx/logs/nginx.pid;
  5. #Specifies the value for maximum file descriptors that can be opened by this process.
  6. worker_rlimit_nofile 65535;
  7. events
  8. {
  9. use epoll;
  10. worker_connections 65535;
  11. }
  12. # http设置
  13. http {
  14. include mime.types;
  15. default_type application/octet-stream;
  16. autoindex on; #自动显示目录
  17. autoindex_exact_size off; #人性化方式显示文件大小否则以byte显示
  18. autoindex_localtime on; #按服务器时间显示,否则以gmt时间显示
  19. # log格式设置
  20. # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. # '$status $body_bytes_sent "$http_referer" '
  22. # '"$http_user_agent" "$http_x_forwarded_for"';
  23. server_names_hash_bucket_size 128;
  24. client_header_buffer_size 32k;
  25. large_client_header_buffers 4 32k;
  26. sendfile on; # 开启高效文件传输模式;下载服务器设置为off
  27. tcp_nopush on; # 默认值即可
  28. tcp_nodelay on; # 默认值即可
  29. keepalive_timeout 60; # 长连接超时时间,单位是秒
  30. send_timeout 20; # 指定响应客户端的超时时间
  31. client_max_body_size 10m;# 允许客户端请求的最大单文件字节数
  32. client_body_buffer_size 128k; # 缓冲区代理缓冲用户端请求的最大字节数
  33. # FastCGI设置
  34. fastcgi_connect_timeout 300;
  35. fastcgi_send_timeout 300;
  36. fastcgi_read_timeout 300;
  37. fastcgi_buffer_size 64k;
  38. fastcgi_buffers 4 64k;
  39. fastcgi_busy_buffers_size 128k;
  40. fastcgi_temp_file_write_size 128k;
  41. # gzip压缩功能设置
  42. gzip on; # 开启gzip压缩输出,减少网络传输
  43. gzip_min_length 1k;
  44. gzip_buffers 4 16k;
  45. gzip_http_version 1.0;
  46. gzip_comp_level 2;
  47. gzip_types text/plain application/x-javascript text/css application/xml;
  48. gzip_vary on;
  49. # Nginx带宽限制
  50. #limit_zone crawler $binary_remote_addr 10m;
  51. # http_proxy设置,按需设置
  52. proxy_connect_timeout 75; # 代理连接超时
  53. proxy_send_timeout 75; # 代理发送超时
  54. proxy_read_timeout 75; # 代理接收超时
  55. proxy_buffer_size 4k;
  56. proxy_buffers 4 32k;
  57. proxy_busy_buffers_size 64k;
  58. proxy_temp_file_write_size 64k;
  59. proxy_temp_path /www/server/nginx/proxy_temp 1 2;
  60. #利用proxy_cache来缓存文件
  61. #levels设置目录层次
  62. #keys_zone设置缓存名字和共享内存大小
  63. #inactive在指定时间内没人访问则被删除在这里是1天
  64. #max_size最大缓存空间
  65. proxy_cache_path /www/server/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
  66. #等号中间要加的,关键只要加上proxy_cache_path
  67. # 设定负载均衡后台服务器列表,按需设置,一般不需要
  68. upstream backend {
  69. #ip_hash;
  70. server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
  71. server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
  72. }
  73. # 虚拟主机配置,server 指令开始
  74. include /www/server/nginx/conf/vhosts/*.conf;
  75. }

完整的vhost的配置文件:
/www/server/nginx/conf/vhosts/me.52fhy.com.conf

  1. #虚拟主机配置
  2. server {
  3. listen 80;
  4. server_name me.52fhy.com;
  5. index index.php index.html index.htm;
  6. root /52fhy.com/wordpress/;
  7. # WordPress Rewrite
  8. location / {
  9. if (-f $request_filename/index.html){
  10. rewrite (.*) $1/index.html break;
  11. }
  12. if (-f $request_filename/index.php){
  13. rewrite (.*) $1/index.php;
  14. }
  15. if (!-f $request_filename){
  16. rewrite (.*) /index.php;
  17. }
  18. }
  19. # 解析php|php5后缀
  20. location ~ .*\.(php|php5)?$
  21. {
  22. #fastcgi_pass unix:/tmp/php-cgi.sock;
  23. fastcgi_pass 127.0.0.1:9000;
  24. fastcgi_index index.php;
  25. include fastcgi.conf;
  26. }
  27. # 设置gif|jpg|jpeg|png|bmp|swf文件缓存时间
  28. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  29. {
  30. expires 30d; # 缓存30天
  31. }
  32. # 设置js|css文件缓存时间
  33. location ~ .*\.(js|css)?$
  34. {
  35. expires 1h; # 缓存1小时
  36. }
  37. # 访问日志
  38. access_log /www/log/me.52fhy.com.log;
  39. }

参考:
1、Nginx 服务器安装及配置文件详解
http://www.runoob.com/w3cnote/nginx-install-and-config.html#rd
2、nginx配置location总结及rewrite规则写法 | Sean's Notes
http://seanlook.com/2015/05/17/nginx-location-rewrite/
3、Nginx 图片 js文件缓存配置方法-nginx-操作系统-壹聚教程网
http://www.111cn.net/sys/nginx/64074.htm
4、nginx利用proxy_cache来缓存文件
http://blog.51yip.com/apachenginx/1018.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注