Nginx配置文件說明

在此記錄下Nginx服務(wù)器nginx.conf的配置文件說明, 部分注釋收集與網(wǎng)絡(luò).

#運行用戶

user www-data;?

#啟動進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等

worker_processes? 1;

#全局錯誤日志及PID文件

error_log? /var/log/nginx/error.log;

pid??????? /var/run/nginx.pid;

#工作模式及連接數(shù)上限

events {

use?? epoll;???????????? #epoll是多路復(fù)用IO(I/O Multiplexing)中的一種方式,但是僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能

worker_connections? 1024;#單個后臺worker process進(jìn)程的最大并發(fā)鏈接數(shù)

# multi_accept on;?

}

#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持

http {

#設(shè)定mime類型,類型由mime.type文件定義

include?????? /etc/nginx/mime.types;

default_type? application/octet-stream;

#設(shè)定日志格式

access_log??? /var/log/nginx/access.log;

#sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,對于普通應(yīng)用,

#必須設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為 off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的uptime.

sendfile??????? on;

#tcp_nopush???? on;

#連接超時時間

#keepalive_timeout? 0;

keepalive_timeout? 65;

tcp_nodelay??????? on;


#開啟gzip壓縮

gzip? on;

gzip_disable "MSIE [1-6]\.(?!.*SV1)";

#設(shè)定請求緩沖

client_header_buffer_size??? 1k;

large_client_header_buffers? 4 4k;

include /etc/nginx/conf.d/*.conf;

include /etc/nginx/sites-enabled/*;

#設(shè)定負(fù)載均衡的服務(wù)器列表

upstream mysvr {

#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大

#本機(jī)上的Squid開啟3128端口

server 192.168.8.1:3128 weight=5;

server 192.168.8.2:80? weight=1;

server 192.168.8.3:80? weight=6;

}

server {

#偵聽80端口

listen?????? 80;

#定義使用www.xx.com訪問

server_name? www.xx.com;

#設(shè)定本虛擬主機(jī)的訪問日志

access_log? logs/www.xx.com.access.log? main;

#默認(rèn)請求

location / {

root?? /root;????? #定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置

index index.php index.html index.htm;?? #定義首頁索引文件的名稱

fastcgi_pass? www.xx.com;

fastcgi_param? SCRIPT_FILENAME? $document_root/$fastcgi_script_name;?

include /etc/nginx/fastcgi_params;

}

# 定義錯誤提示頁面

error_page?? 500 502 503 504 /50x.html;?

location = /50x.html {

root?? /root;

}

#靜態(tài)文件,nginx自己處理

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

root /var/www/virtual/htdocs;

#過期30天,靜態(tài)文件不怎么更新,過期可以設(shè)大一點,如果頻繁更新,則可以設(shè)置得小一點。

expires 30d;

}

#PHP 腳本請求全部轉(zhuǎn)發(fā)到 FastCGI處理. 使用FastCGI默認(rèn)配置.

location ~ \.php$ {

root /root;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;

include fastcgi_params;

}

#設(shè)定查看Nginx狀態(tài)的地址

location /NginxStatus {

stub_status??????????? on;

access_log????????????? on;

auth_basic????????????? "NginxStatus";

auth_basic_user_file? conf/htpasswd;

}

#禁止訪問 .htxxx 文件

location ~ /\.ht {

deny all;

}


}

}

以上是一些基本的配置,使用Nginx最大的好處就是負(fù)載均衡

如果要使用負(fù)載均衡的話,可以修改配置http節(jié)點如下:

#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持

http {

#設(shè)定mime類型,類型由mime.type文件定義

include?????? /etc/nginx/mime.types;

default_type? application/octet-stream;

#設(shè)定日志格式

access_log??? /var/log/nginx/access.log;

??? #省略上文有的一些配置節(jié)點

??? #。。。。。。。。。。

#設(shè)定負(fù)載均衡的服務(wù)器列表

upstream mysvr {

#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大

server 192.168.8.1x:3128 weight=5;#本機(jī)上的Squid開啟3128端口

server 192.168.8.2x:80? weight=1;

server 192.168.8.3x:80? weight=6;

}

upstream mysvr2 {

#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大

server 192.168.8.x:80? weight=1;

server 192.168.8.x:80? weight=6;

}

#第一個虛擬服務(wù)器

server {

#偵聽192.168.8.x的80端口

listen?????? 80;

server_name? 192.168.8.x;

#對aspx后綴的進(jìn)行負(fù)載均衡請求

location ~ .*\.aspx$ {

root ? /root; ? ?? #定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置

index index.php index.html index.htm;?? #定義首頁索引文件的名稱

????????? proxy_pass? http://mysvr ;#請求轉(zhuǎn)向mysvr 定義的服務(wù)器列表

????????? #以下是一些反向代理的配置可刪除.

????????? proxy_redirect off;

#后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實IP

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;??? #允許客戶端請求的最大單文件字節(jié)數(shù)

client_body_buffer_size 128k;? #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),

proxy_connect_timeout 90;? #nginx跟后端服務(wù)器連接超時時間(代理連接超時)

proxy_send_timeout 90;??????? #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)

proxy_read_timeout 90;???????? #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時)

proxy_buffer_size 4k;???????????? #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小

proxy_buffers 4 32k;?????????????? #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置

proxy_busy_buffers_size 64k;??? #高負(fù)荷下緩沖大?。╬roxy_buffers*2)

proxy_temp_file_write_size 64k;? #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳

?????? }

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容