Nginx的配置文件通常位于/etc/nginx/nginx.conf
,這是主配置文件。你可以在這個文件中定義全局設置,包括工作進程的數量、錯誤日志的位置等。此外,你還可以在這個文件中包含其他配置文件。
以下是一個基本的Nginx配置文件示例:
# 用戶和組設置
user nginx;
worker_processes auto; # 根據CPU核心數自動設置工作進程數
# 錯誤日志路徑
error_log /var/log/nginx/error.log warn;
# 進程文件路徑
pid /var/run/nginx.pid;
# 事件模塊配置
events {
worker_connections 1024; # 每個工作進程允許的最大并發連接數
}
# HTTP服務器配置
http {
include /etc/nginx/mime.types; # 文件擴展名與MIME類型映射表
default_type application/octet-stream; # 默認MIME類型
# 日志格式設置
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;
# 發送file請求到fastcgi
sendfile on;
keepalive_timeout 65;
# 虛擬主機配置
include /etc/nginx/conf.d/*.conf; # 包含所有虛擬主機配置文件
include /etc/nginx/sites-enabled/*; # 包含所有啟用的虛擬主機配置文件
}
這個配置文件定義了一個基本的HTTP服務器設置,包括用戶、工作進程、錯誤日志、事件模塊和HTTP服務器配置。你可以在http
塊中添加更多的配置,例如靜態文件服務、反向代理、負載均衡等。
此外,你還可以創建虛擬主機配置文件,這些文件通常位于/etc/nginx/conf.d/
或/etc/nginx/sites-available/
目錄下。以下是一個簡單的虛擬主機配置文件示例:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
這個虛擬主機配置文件定義了一個監聽80端口的服務器,服務器名稱為example.com
和www.example.com
。它將請求的根目錄設置為/var/www/html/example.com
,并嘗試查找請求的文件。如果找不到文件,則返回404錯誤。此外,它還定義了一個自定義的錯誤頁面,用于顯示500、502、503和504錯誤。
請注意,這只是一個基本的示例,你可以根據自己的需求進行修改和擴展。在修改配置文件后,記得重新加載或重啟Nginx服務以使更改生效:
sudo nginx -s reload
或者
sudo systemctl reload nginx
希望這些信息對你有所幫助!如果你有其他問題,請隨時提問。