NGINX学习笔记(十):一篇搞懂NGINX配置

秒速五厘米 2023-10-08 19:42 128阅读 0赞

配置文件

  1. # 配置运行NGINX工作节点的用户(组)
  2. # 如果user指令不配置或者配置为 user nobody nobody, 则默认所有用户都可以启动NGINX进程
  3. user nobody nobody;
  4. # NGINX工作节点并发处理能力的关键配置, 通常设置成和CPU的核数相等, 指令格式: worker_processes number|auto;
  5. worker_processes 3;
  6. #全局错误日志及PID文件
  7. #error_log logs/error.log;
  8. #error_log logs/error.log notice;
  9. #error_log logs/error.log info;
  10. #pid logs/nginx.pid;
  11. events {
  12. # epoll是多路复用IO(I/O Multiplexing)中的一种方式, 仅用于linux2.6以上内核, 可以大大提高NGINX的性能
  13. # 一般采用系统默认
  14. use epoll;
  15. # 允许每一个WORKER进程可以同时开启的最大连接数。
  16. # worker_connections 的值需根据 worker_processes 进程数 和 系统可以打开的最大文件总数 进行适当地设置。
  17. worker_connections 1024;
  18. }
  19. http {
  20. # 设定日志格式, 引入其他配置(mime.types中指定了浏览器能够识别的MIME类型以及对应类型的文件后缀名)
  21. include mime.types;
  22. default_type application/octet-stream;
  23. # 设定日志格式
  24. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  25. '$status $body_bytes_sent "$http_referer" '
  26. '"$http_user_agent" "$http_x_forwarded_for"';
  27. # 配置访问日志的存放路径和格式
  28. access_log logs/access.log main;
  29. # 用于开启/关闭使用sendfile()传输文件,默认off
  30. # 对于普通应用, 必须设为 on, 如果用来进行下载等应用磁盘IO重负载的应用时, 可设置为 off,
  31. # 以平衡磁盘与网络I/O处理速度, 降低系统的uptime
  32. sendfile on;
  33. # 表示服务端对连接的保持时间, 默认75秒
  34. keepalive_timeout 65;
  35. #开启gzip压缩
  36. gzip on;
  37. gzip_disable "MSIE [1-6].";
  38. #设定请求缓冲
  39. client_header_buffer_size 128k;
  40. large_client_header_buffers 4 128k;
  41. server {
  42. # 用于网络监听, 一般是两种配置方式:
  43. # listen IP[:PORT]; 或者 listen PORT;
  44. listen 8081;
  45. server_name 192.168.31.111;
  46. # 设定服务器的默认网站根目录位置
  47. root mallx/html;
  48. # 设定本虚拟主机的访问日志
  49. access_log /opt/mallx/server1/log/access.log;
  50. error_page 404 /404.html;
  51. location /server1/location1 {
  52. root /opt/mallx/server1;
  53. # 设置网站的默认首页, 可以设置多个用空格隔开的文件名, 首先找到哪个页面, 就使用哪个页面响应请求
  54. index index1.server1.html index2.server1.html;
  55. }
  56. location /server1/location2 {
  57. root /opt/mallx/server1;
  58. index index1.server1.html index2.server1.html;
  59. }
  60. # 设置静态文件
  61. location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  62. # 过期30天, 静态文件不怎么更新, 过期可以设大一点。如果频繁更新, 则可以设置得小一点。
  63. expires 30d;
  64. }
  65. # 禁止访问所有的.htXXXXX的 文件
  66. location ~ /.ht {
  67. deny all;
  68. }
  69. }
  70. server {
  71. listen 8082;
  72. server_name 192.168.31.111;
  73. access_log /opt/mallx/server2/log/access.log;
  74. error_page 404 /404.html;
  75. location /server2/location1 {
  76. root /opt/mallx/server1;
  77. index index1.server2.html index2.server2.html;
  78. }
  79. location /server2/location2 {
  80. alias /codesheep/webserver/server2/location2/;
  81. index index1.server2.html index2.server2.html;
  82. }
  83. location = /404.html {
  84. root root /opt/mallx/;
  85. index 404.html;
  86. }
  87. }
  88. }

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读