# 指定Nginx进程运行的用户,通常是 nginx 用户。这是一个安全措施,以确保Nginx进程以较低的特权级别运行。 user nginx; # 表示Nginx会自动根据可用的CPU核心数量启动多个工作进程来处理请求 worker_processes auto; # 指定Nginx的错误日志文件的路径和日志级别。在这个示例中,错误日志的级别设置为 notice error_log /var/log/nginx/error.log notice; # 指定Nginx主进程的PID文件路径 pid /var/run/nginx.pid; events { # 设置每个工作进程能够同时处理的最大连接数 worker_connections 1024; } http { # 引入了 MIME 类型配置文件,用于指定文件扩展名与 MIME 类型的映射关系 include /etc/nginx/mime.types; # 如果请求的文件类型无法匹配到具体的MIME类型,将使用 application/octet-stream 作为默认的MIME类型。 default_type application/octet-stream; # 定义了访问日志的格式,其中包含了各种请求信息,如客户端IP、请求时间、请求内容等 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 /var/log/nginx/access.log main; # 启用了 sendfile 功能,它可以加速文件传输 sendfile on; # 堆代码 duidaima.com # tcp_nopush on; # 设置了Keep-Alive超时时间,即HTTP长连接的超时时间 keepalive_timeout 65; # gzip on; # 引入了 /etc/nginx/conf.d/ 目录下的所有 .conf 文件,通常这些文件包含了虚拟主机配置和其他站点配置 include /etc/nginx/conf.d/*.conf; }标准Nginx配置文件 default.conf 示例
server { # listen 80; 和 listen [::]:80; 这两行配置指示Nginx监听HTTP请求的80端口,分别支持IPv4和IPv6。这是Web服务器默认的HTTP端口。 listen 80; listen [::]:80; # 这里指定了虚拟主机的域名,这里是localhost。这意味着这个虚拟主机会响应访问localhost的HTTP请求。 server_name localhost; # access_log /var/log/nginx/host.access.log main; # 这是一个配置块,定义了对于根路径 / 的请求应该如何处理。 location / { # 这指定了Nginx应该在响应这个虚拟主机的请求时从哪里获取文件。在这里,文件将从/usr/share/nginx/html目录中获取。 root /usr/share/nginx/html; # 这里列出了默认的索引文件。如果请求的URL以 / 结尾,Nginx将尝试查找并提供 index.html 或 index.htm 文件。 index index.html index.htm; } # error_page 404 /404.html; # redirect server error pages to the static page /50x.html # 这些配置定义了当发生500系列错误时,Nginx应该显示一个自定义的错误页面 50x.html。这个错误页面位于/usr/share/nginx/html目录下。 error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} }nginx配置文件有哪些配置项
这只是Nginx配置文件中的一些常见配置项示例。Nginx配置非常灵活,可以根据需要定义各种不同的行为和规则。具体的配置取决于你的服务器需求,你可以根据需要在配置文件中添加、修改或删除配置项。要查看完整的Nginx配置选项和详细说明,可以参考官方文档或运行 nginx -h 命令来获取帮助信息。
可以配置多个server吗?
是的,你可以在Nginx配置文件中配置多个 server 块,每个 server 块表示一个虚拟主机或一个服务器块。每个虚拟主机可以处理不同的域名或IP地址的请求,并有自己的配置。这允许你在同一台服务器上托管多个网站或应用程序,并根据主机名或其他条件来路由请求。http { server { listen 80; server_name example1.com www.example1.com; root /var/www/example1; index index.html; location / { # 针对 example1.com 的配置 } } server { listen 80; server_name example2.com; root /var/www/example2; index index.html; location / { # 针对 example2.com 的配置 } } server { listen 80 default_server; server_name _; root /var/www/default; index index.html; location / { # 默认虚拟主机的配置 } } }在上面的示例中,我们配置了三个 server 块: