歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Nginx配置文件nginx.conf詳解

Nginx配置文件nginx.conf詳解

日期:2017/3/3 11:54:29   编辑:Linux技術
一.nginx.conf的組成模塊如下:
events{
.................
}
http{
...............
Server{................ }
Server{................ }...............
https{
...............
}
}
二nginx.conf文件詳解
user nginx nginx;
定義nginx運行的用戶及用戶組
worker_processes 2;
nginx進程數,根據硬件調整,建議設置為等於CPU總核心數或2倍於CPU
error_log logs/error.log;
error_log logs/error.log notice;error_log logs/error.log info;錯誤日志,存放路徑
pid logs/nginx.pid;
pid(進程標識符),存放路徑
worker_rlimit_nofile 204800;
指定進程可以打開的最大描述符:數目。
這個指令是指當一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit -n 的值保持一致。在linux 2.6內核下開啟文件打開數為65535,worker_rlimit_nofile就相應應該填寫65535。這是因為nginx調度時分配請求到進程並不是那麼的均衡,所以假如填寫10240,總並發量達到3-4萬時就有進程可能超過10240了,這時會返回502錯誤。
##配置事件
events
{
use epoll;
使用epoll的I/O 模型,linux建議epoll,FreeBSD建議采用kqueue,window下不指定。補充說明:與apache相類,nginx針對不同的操作系統,有不同的事件模型A)標准事件模型Select、poll屬於標准事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或pollB)高效事件模型Kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成內核崩潰。Epoll:使用於Linux內核2.6版本及以後的系統。
worker_connections 1024;
每個工作進程的最大連接數量。根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%
keepalive_timeout 60;keepalive超時時間
client_header_buffer_size 4k;
客戶端請求頭部的緩沖區大小。這個可以根據系統分頁大小來設置,一般一個請求頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設置為分頁大小。分頁大小可以用命令getconf PAGESIZE 取得。[root@web001 ~]# getconf PAGESIZE4096但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設置為“系統分頁大小”的整倍數
open_file_cache max=65535 inactive=60s;
這個將為打開文件指定緩存,默認是沒有啟用的,max指定緩存數量,建議和打開文件數一致,inactive是指經過多長時間文件沒被請求後刪除緩存。
open_file_cache_valid 80s;這個是指多長時間檢查一次緩存的有效信息
open_file_cache_min_uses 1;open_file_cache指令中的inactive參數時間內文件的最少使用次數,如果超過這個數字,文件描述符一直是在緩存中打開的,如果有一個文件在inactive時間內一次沒被使用,它將被移除。}
##設定http服務器,利用它的反向代理功能提供負載均衡支持http{include mime.types;設定mime類型,類型由mime.type文件定義
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"';log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';日志格式設置。$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;$remote_user:用來記錄客戶端用戶名稱;$time_local: 用來記錄訪問時間與時區;$request: 用來記錄請求的url與http協議;$status: 用來記錄請求狀態;成功是200,$body_bytes_sent :記錄發送給客戶端文件主體內容大小;$http_referer:用來記錄從哪個頁面鏈接訪問過來的;$http_user_agent:記錄客戶浏覽器的相關信息;通常web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務器的iP地址。反向代理服務器在轉發請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。
access_log logs/host.access.log main;access_log logs/host.access.404.log log404;用了log_format指令設置了日志格式之後,需要用access_log指令指定日志文件的存放路徑
sendfile on;sendfile指令指定 nginx 是否調用sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設為on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡IO處理速度,降低系統uptime。 tcp_nopush on;此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用
keepalive_timeout 120;
keepalive超時時間
gzip on;開啟gzip壓縮輸出
upstream server.com {
upstream的負載均衡,此處用weightserver 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}補充:upstream的負載均衡方式:1.輪詢(默認),每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉能自動剔除。這種方式簡便、成本低廉;缺點是:可靠性低和負載分配不均衡,適用於圖片服務器集群和純靜態頁面服務器集群。2.weight(權重),指定輪詢幾率,weight和訪問比率成正比,用於後端服務器性能不均的情況。3.ip_hash(訪問ip),每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。4.fair(第三方),後端服務器的響應時間來分配請求,響應時間短的優先分配,與weight分配策略類似。5.url_hash(第三方),按訪問url的hash結果來分配請求,使每個url定向到同一個後端服務器,後端服務器為緩存時比較有效。
##設定虛擬主機server
{
listen 80;
監聽端口
server_name localhost;
域名可有多個,用空格隔開
charset koi8-r;
編碼方式,默認為utf-8
access_log logs/host.access.log main;
虛擬主機的訪問日志
location / {
root html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
重定向服務器端錯誤到靜態頁面location = /50x.html
{
root html;
}
proxy the PHP scripts to Apache listening on 127.0.0.1:80
代理php腳本到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腳本交給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.conf;
}
deny access to .htaccess files, if Apache's document root concurs with nginx's one
拒絕處理所有.htxxx文件
location ~ /\.ht
{
deny all;
}
}
another virtual host using mix of IP-, name-, and port-based configuration
虛擬主機示例
server {
listen 8000;
listen somename:8080;
server_name somename alias another.alias;
location /
{
root html;
index index.html index.htm;
}
location /NginxStatus
設定查看Nginx狀態的地址{
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
}
##本地動靜分離反向代理配置
location ~ .(jsp|jspx|do)?$
所有jsp的頁面均交由tomcat或resin處理
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
location ~ .*.(htm|html|gif|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$所有靜態文件由nginx直接讀取不經過tomcat或resin{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}
##設定https服務器
HTTPS server
server {
listen 443 ssl;
server_name localhost;
啟用ssl加密
ssl_certificate nginx.pem;
ssl_certificate_key nginx.pem;
證書及密鑰
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location /
{
root html;
index index.html index.htm;
}
}
}
更詳細的模塊參數請參考:http://wiki.nginx.org/Main
nginx官網:http://nginx.org/en/



本文出自 “真水無香” 博客,請務必保留此出處http://chengyanli.blog.51cto.com/11399167/1789731
Copyright © Linux教程網 All Rights Reserved