牛骨文教育服务平台(让学习变的简单)
博文笔记

Nginx简介及配置文件详解

创建时间:2017-03-19 投稿人: 浏览次数:40089
一 Nginx简介    Nginx是一款开源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务
   1.Nginx工作原理        Nginx由内核和模块组成,完成工作是通过查找配置文件将客户端请求映射到一个location block(location是用于URL匹配的命令),location配置的命令会启动不同模块完成工作。
       Nginx模块分为核心模块,基础模块和第三方模块。            核心模块:HTTP模块、EVENT模块(事件)、MAIL模块。
           基础模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块。
           第三方模块:HTTP Upstream Request Hash模块、Notice模块、HTTP Access Key模块。   
   2.性能优势
       web服务器,处理静态文件、索引文件以及自动索引效率高。
       代理服务器,快速高效反向代理,提升网站性能。
       负载均衡器,内部支持Rails和PHP,也可支持HTTP代理服务器,对外进行服务。同时支持简单容错和利用算法进行负载均衡。
       性能方面,Nginx专门为性能设计,实现注重效率。采用Poll模型,可以支持更多的并发连接,并在大并发时占用很低内存。
       稳定性方面,采用分阶段资源分配技术,使CPU资源占用率低。
       高可用性方面,支持热备,启动迅速。
二 Nginx编译安装,配置文件详解    1.Nginx安装
  1. #安装部署nginx所用到的安装工具和相关库
  2. #默认安装的http_rewrite_module(使用正则对请求重写)需pcre库
  3. #默认安装的httP_gzip_module(Gzip压缩)需zlib库
  4. #安装http_ssl_module(HTTPS/SLL)需openssl库
  5. yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
  6. #下载nginx源码包,并解压
  7. wget http://nginx.org/download/nginx-1.10.3.tar.gz
  8. tar -zxvf nginx-1.10.3.tar.gz
  9. cd nginx-1.10.3
  10. #设置参数 参数具体参考《Nginx编译参数》
  11. ./configure
  12. --prefix=/usr/local/nginx
  13. --with-http_ssl_module
  14. #编译并安装 
  15. make && make install
   2.Nginx配置文件(/usr/local/nginx/conf/nginx.conf)
  
   配置文件主要由四部分组成:main(全区设置),server(主机配置),upstream(负载均衡服务器设置),和location(URL匹配特定位置设置)。    1)全局变量
  1. #Nginx的worker进程运行用户以及用户组
  2. #user nobody nobody;
  3. #Nginx开启的进程数
  4. worker_processes 1;
  5. #worker_processes auto;
  6. #以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.
  7. #这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。eg:
  8. #worker_processes 4 #4核CPU
  9. #worker_cpu_affinity 0001 0010 0100 1000
  10. #定义全局错误日志定义类型,[debug|info|notice|warn|crit]
  11. #error_log logs/error.log info;
  12. #指定进程ID存储文件位置
  13. #pid logs/nginx.pid;
  14. #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
  15. #vim /etc/security/limits.conf
  16. # * soft nproc 65535
  17. # * hard nproc 65535
  18. # * soft nofile 65535
  19. # * hard nofile 65535
  20. worker_rlimit_nofile 65535;
    2)事件配置
  1. events {
  2. #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  3. use epoll;
  4. #每个进程可以处理的最大连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。理论值:worker_rlimit_nofile/worker_processes
  5. #注意:最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处
  6. worker_connections 65535;
  7. #worker工作方式:串行(一定程度降低负载,但服务器吞吐量大时,关闭使用并行方式)
  8. #multi_accept on;
  9. }
   3)http参数
  1. #文件扩展名与文件类型映射表
  2. include mime.types;
  3. #默认文件类型
  4. default_type application/octet-stream;
  5. #日志相关定义
  6. #log_format main "$remote_addr - $remote_user [$time_local] "$request" "
  7. # "$status $body_bytes_sent "$http_referer" "
  8. # ""$http_user_agent" "$http_x_forwarded_for"";
  9. #定义日志的格式。后面定义要输出的内容。
  10. #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
  11. #2.$remote_user :用来记录客户端用户名称;
  12. #3.$time_local :用来记录访问时间与时区;
  13. #4.$request :用来记录请求的url与http协议;
  14. #5.$status :用来记录请求状态;
  15. #6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
  16. #7.$http_referer :用来记录从那个页面链接访问过来的;
  17. #8.$http_user_agent :记录客户端浏览器的相关信息
  18. #连接日志的路径,指定的日志格式放在最后。
  19. #access_log logs/access.log main;
  20. #只记录更为严重的错误日志,减少IO压力
  21. error_log logs/error.log crit;
  22. #关闭日志
  23. #access_log off;
  24. #默认编码
  25. #charset utf-8;
  26. #服务器名字的hash表大小
  27. server_names_hash_bucket_size 128;
  28. #客户端请求单个文件的最大字节数
  29. client_max_body_size 8m;
  30. #指定来自客户端请求头的hearerbuffer大小
  31. client_header_buffer_size 32k;
  32. #指定客户端请求中较大的消息头的缓存最大数量和大小。
  33. large_client_header_buffers 4 64k;
  34. #开启高效传输模式。
  35. sendfile on;
  36. #防止网络阻塞
  37. tcp_nopush on;
  38. tcp_nodelay on;    
  39. #客户端连接超时时间,单位是秒
  40. keepalive_timeout 60;
  41.    #客户端请求头读取超时时间
  42.    client_header_timeout 10;
  43.    #设置客户端请求主体读取超时时间
  44.    client_body_timeout 10;
  45.    #响应客户端超时时间
  46.   send_timeout 10;
  47. #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
  48. fastcgi_connect_timeout 300;
  49. fastcgi_send_timeout 300;
  50. fastcgi_read_timeout 300;
  51. fastcgi_buffer_size 64k;
  52. fastcgi_buffers 4 64k;
  53. fastcgi_busy_buffers_size 128k;
  54. fastcgi_temp_file_write_size 128k;
  55. #gzip模块设置
  56. #开启gzip压缩输出
  57. gzip on;
  58. #最小压缩文件大小
  59. gzip_min_length 1k;
  60. #压缩缓冲区
  61. gzip_buffers 4 16k;
  62. #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
  63. gzip_http_version 1.0;
  64. #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
  65. gzip_comp_level 2;
  66. #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
  67. gzip_types text/plain application/x-javascript text/css application/xml;
  68.    #前端缓存服务器缓存经过压缩的页面
  69.  gzip_vary on;
    4)虚拟主机基本设置
  1. #虚拟主机定义
  2. server {
  3.        #监听端口
  4. listen 80;
  5.        #访问域名
  6. server_name localhost;
  7.        #编码格式,若网页格式与此不同,将被自动转码
  8. #charset koi8-r;
  9.        #虚拟主机访问日志定义
  10. #access_log logs/host.access.log main;
  11.        #对URL进行匹配
  12. location / {
  13.            #访问路径,可相对也可绝对路径
  14. root html;
  15.            #首页文件。以下按顺序匹配
  16. index index.html index.htm;
  17. }
  18. #错误信息返回页面
  19. #error_page 404 /404.html;
  20. # redirect server error pages to the static page /50x.html
  21. #
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. #访问URL以.php结尾则自动转交给127.0.0.1
  27. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  28. #
  29. #location ~ .php$ {
  30. # proxy_pass http://127.0.0.1;
  31. #}
  32. #php脚本请求全部转发给FastCGI处理
  33. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  34. #
  35. #location ~ .php$ {
  36. # root html;
  37. # fastcgi_pass 127.0.0.1:9000;
  38. # fastcgi_index index.php;
  39. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  40. # include fastcgi_params;
  41. #}
  42. #禁止访问.ht页面 (需ngx_http_access_module模块)
  43. # deny access to .htaccess files, if Apache"s document root
  44. # concurs with nginx"s one
  45. #
  46. #location ~ /.ht {
  47. # deny all;
  48. #}
  49. }
  50. #HTTPS虚拟主机定义
  51. # HTTPS server
  52. #
  53. #server {
  54. # listen 443 ssl;
  55. # server_name localhost;
  56. # ssl_certificate cert.pem;
  57. # ssl_certificate_key cert.key;
  58. # ssl_session_cache shared:SSL:1m;
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。