nginx配置檔案中文詳解
阿新 • • 發佈:2019-02-18
1、nginx的配置檔案中文詳解如下:
#user nginx;#nginx程序數,建議設定為等於CPU總核心數。
worker_processes 10;
#程序檔案
pid logs/nginx.pid;
{
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高效能網路I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
# 單個程序最大連線數(最大連線數=連線數*程序數)
worker_connections 65535;
}
http # 設定http伺服器
{
include mime.types; # 副檔名與檔案型別對映表
default_type application/octet-stream; # 預設檔案型別
sendfile on;
keepalive_timeout 65; # 長連線超時時間,單位是秒
#gzip on; #壓縮,提升效能
ignore_invalid_headers off #若不關閉,帶下劃線的head將丟失
underscores_in_headers on;
client_max_body_size 10m;#允許客戶端請求的最大單檔案位元組數
upstream images
{
#upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth引數表示權值,權值越高被分配到的機率越大。
ip_hash;
server 192.168.1.123:7900 weight=3;
server 192.168.1.124:7900 weight=2;
}
upstream app
#負載均衡
{
ip_hash;
server 192.168.1.123:80 weight=2;
server 192.168.1.124:80 weight=3;
}
}
server
{
#監聽埠
listen 8080;
listen 80;
#對 "/" 啟用反向代理
location /
{
if ($request_uri ~ "^\/test\/method1\/")
{
break;
proxy_pass http://app;
}
if ($request_uri ~ "^\/test\/method2\/")
{
break;
proxy_pass http://images;
}
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web伺服器可以通過X-Forwarded-For獲取使用者真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#日誌格式設定
log_format my_access_log
'[$time_local] - $server_addr:$server_port '
'$request_method$uri$args '
'- $upstream_addr$server_protocol [$http_user_agent] [$http_cookie] $http_referer '
'$host$status00$bytes_sent$request_length0'
'"$upstream_cache_status"';
log_format my_access_log2
'$time_local - $upstream_addr$server_addr:$server_port '
'$request_method$uri$args '
'- $remote_addr$server_protocol [$http_user_agent] [$http_cookie] $http_referer '
'$host$status00$bytes_sent$request_length0'
'"$upstream_cache_status"';
access_log /app/nginx/logs/access.log my_access_log2;
error_log /app/nginx/logs/error.log info;
#定義本虛擬主機的訪問日誌
log_format error_log '$server_name$remote_addr - $remote_user [$time_local] "$request" '
'$status$body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
' $upstream_addr$request_time$upstream_response_time';
}
2、nginx啟動、重啟、關閉:
從容停止nginx:kill -QUIT 主程序號
快速停止nginx:kill -TERM 主程序號
強制停止nginx:pkill -9 nginx
平滑重啟命令:kill -HUP 程序號或程序號檔案路徑
或者使用:./nginx -s reload