1.nginx.conf主要由三部分组成

  • 全局块:从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
  • events块:events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。 这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
  • http块:算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。http 块也可以包括 http全局块、server 块。 ```nginx

    user nobody;

    worker_processes 2; //开启的nginx进程数量,通常是随cpu的核心数一致

error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events { //events 块 worker_connections 50000; // 每个nginx进程支持的并发访问量。 }

http { //http块 include mime.types; default_type application/octet-stream;

log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘

‘$status $body_bytes_sent “$http_referer” ‘

‘“$http_user_agent” “$http_x_forwarded_for”‘;

access_log logs/access.log main;

sendfile on;

tcp_nopush on;

keepalive_timeout 0;

keepalive_timeout 65;

gzip on;

upstream web {

  1. server 192.168.99.100:80;
  2. server 192.168.99.200:80;

} server { listen 80; server_name localhost;

  1. #charset koi8-r;
  2. #access_log logs/host.access.log main;
  3. location / {
  4. proxy_pass http://web;
  5. root html;
  6. index index.html index.htm;
  7. }
  8. #error_page 404 /404.html;
  9. # redirect server error pages to the static page /50x.html
  10. #
  11. error_page 500 502 503 504 /50x.html;
  12. location = /50x.html {
  13. root html;
  14. }
  15. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  16. #
  17. #location ~ \.php$ {
  18. # proxy_pass http://127.0.0.1;
  19. #}
  20. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  21. #
  22. #location ~ \.php$ {
  23. # root html;
  24. # fastcgi_pass 127.0.0.1:9000;
  25. # fastcgi_index index.php;
  26. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  27. # include fastcgi_params;
  28. #}
  29. # deny access to .htaccess files, if Apache's document root
  30. # concurs with nginx's one
  31. #
  32. #location ~ /\.ht {
  33. # deny all;
  34. #}

}

another virtual host using mix of IP-, name-, and port-based configuration

#

server {

listen 8000;

listen somename:8080;

server_name somename alias another.alias;

location / {

root html;

index index.html index.htm;

}

}

HTTPS server

#

server {

listen 443 ssl;

server_name localhost;

ssl_certificate cert.pem;

ssl_certificate_key cert.key;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

root html;

index index.html index.htm;

}

}

}

```