Nginx 设置文件详解

[复制链接]
发表于 2026-2-2 17:40:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Nginx 设置文件详解

Nginx 是一款高性能的HTTP和反向署理服务器,也是一个IMAP/POP3/SMTP署理服务器。在生产环境中,Nginx常用于处置处罚大量并发哀求和负载平衡。其设置文件通常位于 /etc/nginx/nginx.conf 或 /usr/local/nginx/conf/nginx.conf。本文将具体先容其设置文件布局及常用的设置指令,以资助你更好地明确和利用Nginx。
Nginx 设置文件布局

Nginx设置文件的根本布局包罗以下几个部门:

  • 全局设置(Main Context)
  • 变乱设置(Events Context)
  • HTTP设置(HTTP Context)

    • 服务器设置(Server Context)
    • 位置设置(Location Context)

全局设置

全局设置部门用于设置Nginx服务器的全局参数,如用户、工作历程数、历程权限等。
  1. user www-data;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
复制代码

  • user: 指定Nginx工作历程的用户和组。
  • worker_processes: 指定工作历程的数量,auto 表现自动检测CPU焦点数。
  • error_log: 设置错误日记文件路径和日记级别。
  • pid: 指定存储Nginx主历程ID的文件路径。
变乱设置

变乱设置部门用于处置处罚Nginx服务器的工作毗连数和毗连处置处罚方式。
  1. events {
  2.     worker_connections 1024;
  3.     use epoll;
  4. }
复制代码

  • worker_connections: 指定每个工作历程的最大毗连数。
  • use: 指定变乱驱动模子(如epoll、kqueue等)。
HTTP设置

HTTP设置部门险些涵盖了Nginx的全部HTTP相干设置。它包罗HTTP服务器的全局设置、服务器块(Server Blocks)以及位置块(Location Blocks)。
  1. http {
  2.     include /etc/nginx/mime.types;
  3.     default_type application/octet-stream;
  4.     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5.                       '$status $body_bytes_sent "$http_referer" '
  6.                       '"$http_user_agent" "$http_x_forwarded_for"';
  7.     access_log /var/log/nginx/access.log main;
  8.     sendfile on;
  9.     tcp_nopush on;
  10.     tcp_nodelay on;
  11.     keepalive_timeout 65;
  12.     include /etc/nginx/conf.d/*.conf;
  13. }
复制代码

  • include: 用于包罗其他设置文件。
  • default_type: 指定默认的MIME范例。
  • log_format: 自界说日记格式。
  • access_log: 指定访问日记文件及利用的日记格式。
  • sendfile: 开启高效文件传输。
  • tcp_nopush: 优化TCP传输。
  • tcp_nodelay: 镌汰网络延长。
  • keepalive_timeout: 指定毗连超时时间。
服务器设置

服务器设置部门界说了假造主机的设置,每个server块代表一个假造主机。
  1. server {
  2.     listen 80;
  3.     server_name example.com www.example.com;
  4.     root /usr/share/nginx/html;
  5.     index index.html index.htm;
  6.     location / {
  7.         try_files $uri $uri/ =404;
  8.     }
  9.     location /images/ {
  10.         alias /data/images/;
  11.     }
  12.    
  13.     error_page 404 /404.html;
  14.     location = /404.html {
  15.         internal;
  16.     }
  17.     location ~ \.php$ {
  18.         fastcgi_pass 127.0.0.1:9000;
  19.         fastcgi_index index.php;
  20.         include fastcgi_params;
  21.     }
  22. }
复制代码

  • listen: 指定监听端口。
  • server_name: 界说服务器名称。
  • root: 设置根目次。
  • index: 指定默认文件。
  • location: 界说URL匹配规则和处置处罚方式。
  • try_files: 实验次序文件访问。
  • alias: 为特定目次指定路径别名。
  • error_page: 自界说错误页面。
  • fastcgi_pass: 指定PHP-FPM后端服务器。
位置设置(Location Context)

location 块用于处置处罚URL哀求,其匹配规则分为准确匹配、前缀匹配和正则匹配。以下是一些常见的 location 示例:
准确匹配:
  1. location = /exact_path {
  2.     # 配置指令...
  3. }
复制代码
前缀匹配:
  1. location /prefix {
  2.     # 配置指令...
  3. }
复制代码
正则匹配:
  1. location ~ \.php$ {
  2.     # 配置指令...
  3. }
复制代码
示例设置

以下是一个综合的Nginx设置示例,展示了怎样设置多个假造主机和处置处罚静态文件、反向署理等功能
  1. user www-data;
  2. worker_processes auto;
  3. events {
  4.     worker_connections 1024;
  5. }
  6. http {
  7.     include /etc/nginx/mime.types;
  8.     default_type application/octet-stream;
  9.     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10.                       '$status $body_bytes_sent "$http_referer" '
  11.                       '"$http_user_agent" "$http_x_forwarded_for"';
  12.     access_log /var/log/nginx/access.log main;
  13.     sendfile on;
  14.     tcp_nopush on;
  15.     tcp_nodelay on;
  16.     keepalive_timeout 65;
  17.     include /etc/nginx/conf.d/*.conf;
  18.     upstream backend {
  19.         server 127.0.0.1:8080;
  20.     }
  21.     server {
  22.         listen 80;
  23.         server_name example.com www.example.com;
  24.         root /usr/share/nginx/example;
  25.         location / {
  26.             try_files $uri $uri/ /index.html;
  27.         }
  28.         location /api/ {
  29.             proxy_pass http://backend;
  30.             proxy_set_header Host $host;
  31.             proxy_set_header X-Real-IP $remote_addr;
  32.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  33.             proxy_set_header X-Forwarded-Proto $scheme;
  34.         }
  35.         location ~ /\.ht {
  36.             deny all;
  37.         }
  38.     }
  39.     server {
  40.         listen 80;
  41.         server_name test.com www.test.com;
  42.         root /usr/share/nginx/test;
  43.         location / {
  44.             try_files $uri $uri/ =404;
  45.         }
  46.         location /secure/ {
  47.             auth_basic "Restricted";
  48.             auth_basic_user_file /etc/nginx/.htpasswd;
  49.         }
  50.     }
  51. }
复制代码
结论

Nginx的设置文件固然看起来大概有些复杂,但实际上非常机动和强大。通过公道设置,可以有效地提升Web服务器的性能安全性。渴望这篇详解可以大概资助你更好地把握Nginx设置文件的誊写和利用。如果你在设置Nginx时遇到任何标题或有新的发起,接待留言与我们分享!
Happy Configuring! 🚀

喜欢这篇文章吗?接待分享和关注!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表