# Nginx 的 worker 進程運行用戶
#user nobody;
# worker 進程數(一般爲cpu數)
worker_processes 1;
# 定義全局錯誤日誌定義類型,[debug|info|notice|warn|crit]
#error_log logs/error.log info;
# Nginx進程PID
#pid logs/nginx.pid;
# worker進程能夠打開的最多文件描述數目
#worker_rlimit_nofile 65535;
events {
# Ngin採用的IO模型, [ kqueue | rtsig | epoll | /dev/poll | select | poll ]
use epoll;
# work進程的最大連接數
worker_connections 1024;
#keepalive超時時間。
keepalive_timeout 60;
# 開啓文件緩存,max爲緩存數量,inactive指經過多長時間文件沒被請求後刪除緩存
open_file_cache max=65535 inactive=60s;
# 指多長時間檢查一次緩存的有效信息
open_file_cache_valid 80s;
# open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的
# 例如,如果有一個文件在inactive時間內一次沒被使用,它將被移除。
open_file_cache_min_uses 1;
# 是否在搜索一個文件是記錄cache錯誤
open_file_cache_errors on;
}
http {
# 文件擴展名與文件類型映射表
include mime.types;
#默認文件類型
default_type application/octet-stream;
#默認編碼
charset utf-8;
# 指定日誌格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# $remote_addr、$remote_user這些是Nginx的內置變量
# access日誌的路徑以及輸出格式(記錄請求的日誌)
#access_log logs/access.log main;
# 設定通過nginx上傳文件的大小
client_max_body_size 8m;
# 開啓高效的文件傳輸模式(nginx會採用sendfile 函數[zero copy]來輸出文件)
# 一般應用設置爲on,對應文件傳輸設置爲off(平衡磁盤與網絡IO處理速度,降低系統uptime)
sendfile on;
#gzip on;
# 負載均衡
upstream josiah {
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
}
# 請求在轉發時,設置header信息,這樣代理服務器才能拿到原生的請求
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
server {
listen 80;
server_name localhost;
#charset utf-8;
#access_log logs/host.access.log main;
location / {
# nginx與轉發服務器連接超時時間
proxy_connect_timeout 90;
# nginx與轉發服務器回傳超時時間(代理髮送超時)
proxy_send_timeout 90;
# 連接成功後,後端服務器響應時間(代理接收超時)
proxy_read_timeout 90;
}
}
}
除此之外nginx還可以進行web緩存、請求限流、防盜鏈等
nginx的很多配置可以作用於多個地方(events、server、location),只有作用的範圍不一樣,你可以嘗試根據自己的需求調整配置的位置,使用./sbin/nginx -t命令檢查配置文件是否正確。更多配置請移步Nginx中文網站。