当前位置:首页 > nginx > 正文

nginx配置文件详解(怎么查看nginx配置文件)

  • nginx
  • 2024-03-29 00:08:10
  • 9273
nginx 配置文件详解
nginx 配置文件通常位于 /etc/nginx/nginx.conf。 它是文本文件,由一系列指令组成。
要素
1. 主体块
p
user user_name;
worker_processes number;
error_log path/to/log_file;
pid path/to/pid_file;
events {
worker_connections number;
}
user: 指定运行 nginx 的用户。
worker_processes: 配置子进程的数量,用于处理连接和请求。
error_log: 指定错误日志的位置。
pid: 指定 nginx 进程 ID 文件的位置。
events {} 块:配置与事件处理相关的设置,例如并发的连接数。
2. HTTP 块
p
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
server {
...
}
...
}
include mime.types;: 包含 MIME 类型文件。
default_type application/octet-stream;: 为未定义 MIME 类型的请求设置默认类型。
log_format main ...;: 定义日志格式。
server {} 块:配置特定服务器或虚拟主机的设置。
3. Server 块
p
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
...
}
listen 80;: 指定服务器监听的端口。
server_name example.com;: 指定服务器的主机名或 IP 地址。
root /var/www/example;: 设置 Web 文档的根目录。
index index.html;: 设置默认文档。
4. Location 块
p
location / {
try_files $uri $uri/;
}
location /static {
root /var/www/example/static;
}
location / { ... }: 配置根 URL 路径的设置。
location /static { ... }: 配置 /static 路径的设置。
5. 重写块
p
rewrite ^/old-page$ /new-page permanent;
rewrite: 执行重写规则。
^/old-page$: 要重写的 URL 模式。
/new-page: 重写后的 URL。
permanent: 指示重写是永久性的,并发出 301 响应代码。