1.基本配置

  1. #配置worker进程运行用户 nobody也是一个linux用户,一般用于启动程序,没有密码
  2. user nobody;
  3. #配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量
  4. worker_processes 4;
  5. #配置全局错误日志及类型,[debug | info | notice | warn | error | crit],默认是error
  6. error_log logs/error.log;
  7. #error_log logs/error.log notice;
  8. #error_log logs/error.log info;
  9. pid logs/nginx.pid; #配置进程pid文件

2.events配置

  1. #配置工作模式和连接数
  2. events {
  3. worker_connections 1024; #配置每个worker进程连接数上限,nginx支持的总连接数就等于worker_processes * worker_connections
  4. }

3.http配置

3.1 基本配置

  1. http {
  2. #配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
  3. include mime.types;
  4. #默认文件类型 流类型,可以理解为支持任意类型,假如我们引入的mime.types不能解析,就会使用字节流解析
  5. default_type application/octet-stream;
  6. #配置日志格式,注意这里的main是一个变量,下面有好几个地方用到了main变量
  7. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  8. # '$status $body_bytes_sent "$http_referer" '
  9. # '"$http_user_agent" "$http_x_forwarded_for"';
  10. #配置access.log日志及存放路径,并使用上面定义的main日志格式
  11. #access_log logs/access.log main;
  12. sendfile on; #开启高效文件传输模式,项目上线后开启
  13. #tcp_nopush on; #防止网络阻塞,项目上线后开启
  14. #keepalive_timeout 0;
  15. keepalive_timeout 65; #长连接超时时间,单位是秒
  16. #gzip on; #开启gzip压缩输出,上线后打开,这个是对传输文件进行了压缩,就像我们使用的js.min一样
  17. }

3.2 server配置

  1. #配置虚拟主机
  2. server {
  3. listen 80; #配置监听端口
  4. server_name 112.74.110.26; #配置服务名
  5. #charset koi8-r; #配置字符集
  6. #access_log logs/host.access.log main; #配置本虚拟主机的访问日志
  7. #/在url中代表着 112.74.110.26:80,在linux磁盘中代表着root,/opt/nginx/html
  8. #也就是说访问112.74.110.26:80就是访问/opt/nginx/html
  9. location / {
  10. #html 是默认的nginx目录:/opt/nginx/htm
  11. root html;
  12. #配置首页文件的名称
  13. index index.html index.htm;
  14. }
  15. #error_page 404 /404.html; #配置404页面
  16. # redirect server error pages to the static page /50x.html
  17. #error_page 500 502 503 504 /50x.html; #配置50x错误页面
  18. #精确匹配
  19. location = /50x.html {
  20. root html;
  21. }
  22. #PHP 脚本请求全部转发到Apache处理
  23. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  24. #
  25. #location ~ \.php$ {
  26. # proxy_pass http://127.0.0.1;
  27. #}
  28. #PHP 脚本请求全部转发到FastCGI处理
  29. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  30. #
  31. #location ~ \.php$ {
  32. # root html;
  33. # fastcgi_pass 127.0.0.1:9000;
  34. # fastcgi_index index.php;
  35. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  36. # include fastcgi_params;
  37. #}
  38. #禁止访问 .htaccess 文件
  39. # deny access to .htaccess files, if Apache's document root
  40. # concurs with nginx's one
  41. #
  42. #location ~ /\.ht {
  43. # deny all;
  44. #}
  45. }

4.不常用配置

  1. 0.0.0.0:80表示监听所有网络中对80端口的连接
  2. [::0]:80 ipv6版监听所有网络的80端口