Nginx基本配置文件和變量詳解
1. 基本配置文件
/etc/nginx/nginx.conf
``` # nginx運行的用戶 user nginx; # nginx進程數,建議設置為等於CPU總核心數。 worker_processes 1;全局錯誤日志文件名稱和所在目錄,錯誤日志記錄級別[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log warn;
進程文件nginx.pid所在目錄
pid /var/run/nginx.pid;
一個nginx進程打開的最多文件描述符數目,
理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,
但是nginx分配請求並不均勻,所以建議與ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
events {
# 參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
# epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
# use epoll;
# 單個進程最大連接數(最大連接數 = 連接數 * 進程數)
worker_connections 1024;
# multi_accept on;
}
http服務器配置
http {
# 引入http協議的Content-Type與擴展名對應關系的文件
include /etc/nginx/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" "$http_x_forwarded_for"';
# HTTP服務器請求日志文件的名稱和所在目錄、日志記錄級別
access_log /var/log/nginx/access.log main;
# 開啟高效文件傳輸模式。
# sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為on,
# 如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。
# 注意:如果圖片顯示不正常把這個改成off。
sendfile on;
# 防止網絡阻塞
# tcp_nopush on;
# tcp_nodelay on;
# 開啟目錄列表訪問,合適下載服務器,默認關閉。
# autoindex on;
# 開啟限制IP連接數的時候需要使用
# limit_zone crawler $binary_remote_addr 10m;
# 長連接超時時間,單位是秒
keepalive_timeout 65;
# gzip模塊設置
# 開啟gzip壓縮輸出
# gzip on;
# 最小壓縮文件大小
# gzip_min_length 1k;
# 壓縮緩沖區
# gzip_buffers 4 16k;
# 壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
# gzip_http_version 1.0;
# 壓縮等級
# gzip_comp_level 2;
# 壓縮類型,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
# gzip_types text/plain application/x-javascript text/css application/xml;
# gzip_vary on;
# FastCGI相關參數是為了改善網站的性能:減少資源占用,提高訪問速度。
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 128k;
# fastcgi_buffers 8 128k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
# 允許客戶端請求的最大單文件字節數。
# 如果請求大於指定的大小,則NGINX發回HTTP 413(Request Entity too large)錯誤。
# 如果服務器處理大文件上傳,則該指令非常重要。
# client_max_body_size 20m;
# 用於請求主體的緩沖區大小。
# 如果主體超過緩沖區大小,則完整主體或其一部分將寫入臨時文件。
# 如果NGINX配置為使用文件而不是內存緩沖區,則該指令會被忽略。
# 默認情況下,該指令為32位系統設置一個8k緩沖區,為64位系統設置一個16k緩沖區。
# 該指令在NGINX配置的http,server和location區塊使用。
# client_body_buffer_size 128k;
# upstream abc.com {
# upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
# server 192.168.10.121:80 weight=3;
# server 192.168.10.122:80 weight=2;
# server 192.168.10.123:80 weight=3;
# }
# 引入 '/etc/nginx/conf.d/' 目錄下以 '.conf' 結尾的文件
include /etc/nginx/conf.d/*.conf;
# 虛擬主機的配置
server {
# 監聽端口
listen 80;
# 域名,可以有多個,用空格隔開
server_name localhost;
# 默認編碼
# charset utf-8;
# 日志格式設定
# log_format access '$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/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# 將服務器錯誤頁面重定向到靜態頁面 /50x.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
# 將 PHP 腳本反向代理到監聽 127.0.0.1:80 端口的Apache
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# 將 PHP 腳本反向代理到監聽127.0.0.1:9000端口的FastCGI服務器
#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;
#}
# 如果Apache的文檔根目錄與nginx的相同,拒絕訪問.htaccess文件。
#location ~ /\.ht {
# deny all;
#}
# 圖片緩存時間設置
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
# expires 10d;
# }
# JS和CSS緩存時間設置
# location ~ .*\.(js|css)?$ {
# expires 1h;
# }
# 對 "/" 啟用反向代理
# location / {
# proxy_pass http://127.0.0.1:88;
# proxy_redirect off;
# # 后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# # 以下是一些反向代理的配置,可選。
# proxy_set_header Host $host;
# # nginx跟后端服務器連接超時時間(代理連接超時)
# proxy_connect_timeout 90;
# # 后端服務器數據回傳時間(代理發送超時)
# proxy_send_timeout 90;
# # 連接成功后,后端服務器響應時間(代理接收超時)
# proxy_read_timeout 90;
# # 設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
# proxy_buffer_size 4k;
# # proxy_buffers緩沖區,網頁平均在32k以下的設置
# proxy_buffers 4 32k;
# # 高負荷下緩沖大小(proxy_buffers*2)
# proxy_busy_buffers_size 64k;
# # 設定緩存文件夾大小,大於這個值,將從upstream服務器傳
# proxy_temp_file_write_size 64k;
# }
}
}
<h3>2. Nginx變量</h3>
<h4><strong>HTTP請求變量</strong></h4>
<ul>
<li>
<code>arg_參數名</code>:例如,$arg_userid,可以引用到請求參數userid的值</li>
<li>
<code>http_請求HEADER名</code>:例如,$http_user_agent,可以引用到請求頭信息User-Agent的值</li>
<li>
<code>sent_http_返回HEADER名</code>:在響應客戶端可添加header頭信息</li>
</ul>
<h4><strong>內置變量</strong></h4>
<blockquote>Nginx內置的變量,參考Nginx文檔的<a href="http://nginx.org/en/docs/syslog.html" rel="nofollow noreferrer">Logging to syslog</a>
</blockquote>
<h4><strong>自定義變量</strong></h4>
<p>服務器管理員自己定義的變量</p>
原文地址:https://segmentfault.com/a/1190000015591040