一、配置文件

    1. ## 全局配置 main
    2. # 指定 nginx 运行用户和用户组
    3. user nobody nobody;
    4. # 指定开启的子进程数,一般情况每个耗费10-12m内存,一般指定一个,可以与计算机内核数相关
    5. worker_processes 1;
    6. # 全局错误日志文件 debug(最详细)、info、notice、warn、error、crit(最少)
    7. error_log /usr/local/var/log/nginx/error.log notice;
    8. # 指定进程id的存储文件位置
    9. pid /usr/local/var/run/nginx/nginx.pid;
    10. # 用于指定一个进程可以打开的最多文件描述符数目,使用命令“ulimit -n [number]”来设置
    11. worker_rlimit_nofile 1024;
    12. # nginx工作模式
    13. events {
    14. # 工作模式
    15. # use epoll; # linux 平台,或 select、poll 等,linux 上默认 epoll
    16. # 连接数上限
    17. # 接收前端最大请求数为 worker_processes*worker_connections,作为反向代理时,受最大打开文件数限制
    18. worker_connections 1024;
    19. ....
    20. }
    21. # http设置,配置http服务器相关属性
    22. http {
    23. # 设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。
    24. include mime.types;
    25. # 设定了默认的类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置asp 的locate 环境时,Nginx是不予解析的,此时,用浏览器访问asp文件就会出现下载了
    26. default_type application/octet-stream;
    27. # 用于设置日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型
    28. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    29. '$status $body_bytes_sent "$http_referer" '
    30. '"$http_user_agent" "$http_x_forwarded_for"';
    31. # 用来纪录每次的访问日志的文件地址,后面的main是日志的格式样式,对应于log_format的main
    32. access_log /usr/local/var/log/nginx/access.log main;
    33. # 开启高效文件传输模式。将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞
    34. sendfile on;
    35. tcp_nopush on;
    36. tcp_nodelay on;
    37. # 设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接
    38. keepalive_timeout 10;
    39. ....
    40. upstream myproject {
    41. .....
    42. }
    43. # 定义虚拟主机开始
    44. server {
    45. # 指定服务端口
    46. listen 8080;
    47. # 指定IP地址或者域名,多个域名之间用空格分开
    48. server_name localhost 192.168.12.10 www.yangyi.com;
    49. # 全局定义。表示在这整个server虚拟主机内,全部的root web根目录。注意要和locate {}下面定义的区分开来。
    50. root /Users/yangyi/www;
    51. # 全局定义访问的默认首页地址。注意要和locate {}下面定义的区分开来
    52. index index.php index.html index.htm;
    53. # 设置网页的默认编码格式
    54. charset utf-8;
    55. # 用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式
    56. access_log usr/local/var/log/host.access.log main;
    57. aerror_log usr/local/var/log/host.error.log error;
    58. ....
    59. # 重要模块,与负载均衡、反向代理、虚拟域名等有关,定位、解析url,支持条件判断、正则匹配、过滤处理
    60. # / 表示匹配访问根目录
    61. location / {
    62. # 用于指定访问根目录时,虚拟主机的web目录,这个目录可以是相对路径(相对路径是相对于nginx的安装目录)。也可以是绝对路径
    63. root /usr/mine/www;
    64. # 访问的默认首页地址
    65. index index.php index.html index.htm;
    66. ....
    67. }
    68. # 反向代理
    69. location /itcast/ {
    70. # 将请求代理到指定的服务器上,url可以是主机名或者IP地址加PORT的形式
    71. # proxy_pass url;
    72. proxy_pass http://127.0.0.1:12345;
    73. # 转发时的协议方法名,转发时改为某种请求方式
    74. # proxy_method method_name;
    75. # Nginx会将上游服务器的响应转发给客户端,但默认不转发HTTP头部字段(Date Server X-Pad X-Accel-* ) 使用proxy_hide_header可以指定任意头部不能被转发
    76. # proxy_hide_header Cache-Control;
    77. # proxy_hide_header MicrosoftOfficeWebServer;
    78. # 设置哪些头部允许转发
    79. # proxy_pass_header X-Accel-Redirect;
    80. # 确定上游服务器是否向上游服务器转发HTTP包体 默认 on
    81. # proxy_pass_request_body off|on;
    82. # 确定是否转发HTTP头部 默认 on
    83. # proxy_pass_request_header on | off;
    84. # proxy_redirect [default | off |redirect |replacement]
    85. # 表示上游一台服务器转发请求出现错误时,继续换一套服务器处理这个请求
    86. 其参数用来说明在那些情况下继续选择下一台上游服务器转发请求,默认 proxy_next_upstream error timeout;
    87. # proxy_next_upstream [error |timeout |invalid_header |http_500 |http_502~504 |http_404 | off]
    88. proxy_set_header X-real-ip $remote_addr;
    89. proxy_set_header Host $http_host;
    90. }
    91. # 负载均衡
    92. # 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助。通过某种负载分担技术,将外部发送来的请求按照事先设定分配算法分配到对称结构中的某一台服务器上,而接收到请求的服务器独立地回应客户的请求。
    93. 均衡负载能够平均分配客户请求到服务器列阵,籍此提供快速获取重要数据,解决大量并发访问服务问题。
    94. }
    95. server {
    96. ....
    97. location {
    98. ....
    99. }
    100. }
    101. ....
    102. }