1、配置文件区域划分

Nginx配置文件位于conf下的nginx.conf,大体分为以下几个区域:

  1. # nginx配置文件主要分为六个区域:
  2. # main(全局设置) 作用域是全局
  3. # events(nginx工作模式)
  4. # upstream(负载均衡服务器设置)
  5. # http(http设置)
  6. # sever(主机设置)
  7. # location(URL匹配)
  8. #

2、配置文件描述

  1. ################### main区域 #################################
  2. #user :来指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行。也可以创建nginx用户指定用户。
  3. # 创建www用户,在nginx配置文件中把user noboby noboby;-->user www www;即可
  4. # /usr/sbin/groupadd www
  5. # /usr/sbin/useradd -g www www
  6. #worker_processes:来指定了Nginx要开启的子进程数。每个Nginx进程平均耗费10M~12M内存。根据经验,一般指定1个进程就足够了,如果是多核CPU,
  7. # 建议指定和CPU的数量一样的进程数即可。我这里写2,那么就会开启2个子进程,总共3个进程。
  8. #error_log:用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。
  9. #pid:用来指定进程id的存储文件位置。
  10. #worker_rlimit_nofile:用于指定一个nginx进程可以打开的最多文件描述符数目,这里是65535,需要使用命令“ulimit -n 65535”来设置。
  11. user nobody;
  12. worker_processes 1;
  13. error_log logs/error.log;
  14. error_log logs/error.log notice;
  15. error_log logs/error.log info;
  16. pid logs/nginx.pid;
  17. #####################event 区域###############################
  18. #use:用来指定Nginx的工作模式。Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll。
  19. # 其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,
  20. # 而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选。
  21. #worker_connections:用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。
  22. # 最大客户端连接数由worker_processes和worker_connections决定,即Max_clients=worker_processes*worker_connections,
  23. # 在作为反向代理时,Max_clients变为:Max_clients = worker_processes * worker_connections/4。
  24. # 进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效。
  25. events {
  26. use epoll;
  27. worker_connections 1024;
  28. }
  29. ######################### http设置#####################################
  30. # http模块负责HTTP服务器相关属性的配置,有server和upstream两个子模块
  31. http {
  32. #include :来用设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。
  33. #default_type:设定了默认的类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置asp的locate环境时,Nginx是不予解析的,此时,用浏览器访问asp文件就会出现下载了。
  34. #log_format:用于设置日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型。
  35. include mime.types;
  36. default_type application/octet-stream;
  37. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  38. '$status $body_bytes_sent "$http_referer" '
  39. '"$http_user_agent" "$http_x_forwarded_for"';
  40. access_log logs/access.log main;
  41. sendfile on;
  42. tcp_nopush on;
  43. keepalive_timeout 0;
  44. keepalive_timeout 65;
  45. gzip on;
  46. ######################### server设置#####################################
  47. #server用来定一个虚拟主机,标志定义虚拟主机开始。
  48. #listen:用于指定虚拟主机的服务端口。
  49. #server_name:用来指定IP地址或者域名,多个域名之间用空格分开。
  50. #root :表示在这整个server虚拟主机内,全部的root web根目录。注意要和locate {}下面定义的区分开来。
  51. #index :全局定义访问的默认首页地址。注意要和locate {}下面定义的区分开来。
  52. #charset:用于设置网页的默认编码格式。
  53. #access_log:用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。
  54. server {
  55. listen 80;
  56. server_name localhost;
  57. root /Users/hk/www;
  58. index index.php index.html index.htm;
  59. charset utf-8;
  60. access_log logs/host.access.log main;
  61. aerror_log logs/host.error.log main;
  62. ######################### location设置#####################################
  63. # location模块 负载均衡,反向代理,虚拟域名等配置。是来定位的,定位URL,解析URL,它也提供了强大的正则匹配功能,也支持条件判断匹配,
  64. # 可以通过location指令实现Nginx对动,静态网页进行过滤处理。
  65. #/表示匹配访问根目录。
  66. #root指令用于指定访问根目录时,虚拟主机的web目录,这个目录可以是相对路径(相对路径是相对于nginx的安装目录)。也可以是绝对路径。
  67. #proxy_pass:代理转发,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
  68. #proxy_set_header:允许重新定义或者添加发往后端服务器的请求头。
  69. #include:加载配置文件,后面介绍nginx多个配置文件时候会提到。
  70. #root:定位localtion匹配的url资源路径。
  71. #index:定义页面显示html,一般和alias配合使用。
  72. location / {
  73. root html;
  74. index index.html index.htm;
  75. }
  76. error_page 404 /404.html;
  77. error_page 500 502 503 504 /50x.html;
  78. location = /50x.html {
  79. root html;
  80. }
  81. #反向代理配置
  82. location /jyb {
  83. proxy_pass http://qurt/;
  84. proxy_read_timeout 1800s;
  85. proxy_set_header Host $host:$server_port;
  86. proxy_set_header X-real-ip $remote_addr;
  87. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  88. proxy_set_header X-Forwarded-Proto $scheme;
  89. }
  90. #采用uwsgi方式
  91. location /python/ {
  92. include uwsgi_params;
  93. uwsgi_pass 127.0.0.1:33333;
  94. }
  95. # FastCGI方式
  96. location ~ \.php$ {
  97. root html;
  98. fastcgi_pass 127.0.0.1:9000;
  99. fastcgi_index index.php;
  100. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  101. include fastcgi_params;
  102. }
  103. #访问nginx本机目录的文件
  104. location / {
  105. root /home/hk/;
  106. index index.html index.htm;
  107. }
  108. location /static/ {
  109. alias /var/static/;
  110. }
  111. # deny access to .htaccess files, if Apache's document root
  112. # concurs with nginx's one
  113. #
  114. location ~ /\.ht {
  115. deny all;
  116. }
  117. }
  118. # another virtual host using mix of IP-, name-, and port-based configuration
  119. server {
  120. listen 8000;
  121. listen somename:8080;
  122. server_name somename alias another.alias;
  123. location / {
  124. root html;
  125. index index.html index.htm;
  126. }
  127. }
  128. # HTTPS server
  129. server {
  130. listen 443 ssl;
  131. server_name localhost;
  132. ssl_certificate cert.pem;
  133. ssl_certificate_key cert.key;
  134. ssl_session_cache shared:SSL:1m;
  135. ssl_session_timeout 5m;
  136. ssl_ciphers HIGH:!aNULL:!MD5;
  137. ssl_prefer_server_ciphers on;
  138. location / {
  139. root html;
  140. index index.html index.htm;
  141. }
  142. }
  143. ##############upstram 模块################
  144. # upstream 模块 负载均衡模块,通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。
  145. #Nginx的负载均衡模块目前支持4种调度算法:
  146. # weight 轮询(默认)。每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。
  147. # weight指定轮询权值,weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。
  148. # ip_hash。每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。
  149. # fair。比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,
  150. # 也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。
  151. # url_hash。按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,
  152. # 如果需要使用这种调度算法,必须安装Nginx 的hash软件包。
  153. #在HTTP Upstream模块中,可以通过server指令指定后端服务器的IP地址和端口,同时还可以设定每个后端服务器在负载均衡调度中的状态。常用的状态有:
  154. # down,表示当前的server暂时不参与负载均衡。
  155. # backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
  156. # max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
  157. # fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。
  158. #注意 当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是backup。
  159. #备注: nginx的worker_rlimit_nofile达到上限时,再有客户端链接报502错误. 用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径。
  160. upstream server_group {
  161. ip_hash;
  162. server 192.168.123.1:80;
  163. server 192.168.123.2:80 down;
  164. server 192.168.123.3:8080 max_fails=3 fail_timeout=20s;
  165. server 192.168.123.4:8080;
  166. }
  167. server {
  168. listen 80;
  169. server_name localhost;
  170. location / {
  171. proxy_pass http://server_group/;
  172. }
  173. }
  174. }
  175. ######################nginx 中location中root和alias的区别 ####################
  176. nginx指定文件路径有两种方式rootalias,这两者的用法区别,使用方法总结了。
  177. rootalias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
  178. [root]
  179. 语法:root path
  180. 默认值:root html
  181. 配置段:httpserverlocationif
  182. [alias]
  183. 语法:alias path
  184. 配置段:location
  185. root实例:
  186. location ^~ /t/ {
  187. root /www/root/html/;
  188. }
  189. 如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
  190. alias实例:
  191. location ^~ /t/ {
  192. alias /www/root/html/new_t/;
  193. }
  194. 如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t
  195. 因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
  196. 注意:
  197. 1. 使用alias时,目录名后面一定要加"/"
  198. 2. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
  199. 3. alias只能位于location块中。(root可以不放在location中)