3.1 Nginx的核心配置文件

学习Nginx首先需要对它的核心配置文件有一定的认识,这个文件位于Nginx的安装目录/usr/local/nginx/conf目录下,名字为nginx.conf

image.png

详细配置,可以参考resources目录下的<

  1. #配置worker进程运行用户 nobody也是一个linux用户,一般用于启动程序,没有密码
  2. user nobody;
  3. #配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量
  4. worker_processes 1;
  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文件
  10. ###====================================================
  11. #配置工作模式和连接数
  12. events {
  13. worker_connections 1024; #配置每个worker进程连接数上限,nginx支持的总连接数就等于worker_processes * worker_connections
  14. }
  15. ###===================================================
  16. #配置http服务器,利用它的反向代理功能提供负载均衡支持
  17. http {
  18. #配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
  19. include mime.types;
  20. #默认文件类型 流类型,可以理解为支持任意类型
  21. default_type application/octet-stream;
  22. #配置日志格式
  23. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  24. # '$status $body_bytes_sent "$http_referer" '
  25. # '"$http_user_agent" "$http_x_forwarded_for"';
  26. #配置access.log日志及存放路径,并使用上面定义的main日志格式
  27. #access_log logs/access.log main;
  28. sendfile on; #开启高效文件传输模式
  29. #tcp_nopush on; #防止网络阻塞
  30. #keepalive_timeout 0;
  31. keepalive_timeout 65; #长连接超时时间,单位是秒
  32. #gzip on; #开启gzip压缩输出
  33. ###-----------------------------------------------
  34. #配置虚拟主机
  35. server {
  36. listen 80; #配置监听端口
  37. server_name localhost; #配置服务名
  38. #charset koi8-r; #配置字符集
  39. #access_log logs/host.access.log main; #配置本虚拟主机的访问日志
  40. #默认的匹配斜杠/的请求,当访问路径中有斜杠/,会被该location匹配到并进行处理
  41. location / {
  42. #root是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
  43. root html;
  44. #配置首页文件的名称
  45. index index.html index.htm;
  46. }
  47. #error_page 404 /404.html; #配置404页面
  48. # redirect server error pages to the static page /50x.html
  49. #error_page 500 502 503 504 /50x.html; #配置50x错误页面
  50. #精确匹配
  51. location = /50x.html {
  52. root html;
  53. }
  54. #PHP 脚本请求全部转发到Apache处理
  55. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  56. #
  57. #location ~ \.php$ {
  58. # proxy_pass http://127.0.0.1;
  59. #}
  60. #PHP 脚本请求全部转发到FastCGI处理
  61. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  62. #
  63. #location ~ \.php$ {
  64. # root html;
  65. # fastcgi_pass 127.0.0.1:9000;
  66. # fastcgi_index index.php;
  67. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  68. # include fastcgi_params;
  69. #}
  70. #禁止访问 .htaccess 文件
  71. # deny access to .htaccess files, if Apache's document root
  72. # concurs with nginx's one
  73. #
  74. #location ~ /\.ht {
  75. # deny all;
  76. #}
  77. }
  78. #配置另一个虚拟主机
  79. # another virtual host using mix of IP-, name-, and port-based configuration
  80. #
  81. #server {
  82. # listen 8000;
  83. # listen somename:8080;
  84. # server_name somename alias another.alias;
  85. # location / {
  86. # root html;
  87. # index index.html index.htm;
  88. # }
  89. #}
  90. #配置https服务,安全的网络传输协议,加密传输,端口443,运维来配置
  91. #
  92. # HTTPS server
  93. #
  94. #server {
  95. # listen 443 ssl;
  96. # server_name localhost;
  97. # ssl_certificate cert.pem;
  98. # ssl_certificate_key cert.key;
  99. # ssl_session_cache shared:SSL:1m;
  100. # ssl_session_timeout 5m;
  101. # ssl_ciphers HIGH:!aNULL:!MD5;
  102. # ssl_prefer_server_ciphers on;
  103. # location / {
  104. # root html;
  105. # index index.html index.htm;
  106. # }
  107. #}
  108. }

Nginx的核心配置文件主要由三个部分构成

3.1.1 基本配置

image.png

3.3.2 events配置

image.png

3.3.3 http配置

(1)基本配置

image.png

(2)server配置,可以有多个

image.png

3.2 Nginx主要应用

  • 静态网站部署
  • 负载均衡
  • 静态代理
  • 动静分离
  • 虚拟主机