歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Nginx.conf 詳解

Nginx.conf 詳解

日期:2017/3/2 10:03:16   编辑:關於Linux

#使用哪個用戶啟動nginx 前面是用戶,後面是組

user www www;

#nginx工作的進程數量(通常等於CPU數量或者2倍於CPU)

worker_processes 2;

# [ debug | info | notice | warn | error | crit ] 錯誤日志的位置

error_log /var/htdocs/logs/nginx_error.log crit;

#進程號保存文件

pid /usr/local/nginx/nginx.pid;

#指定最大的文件描述符值,可以通過這個進程打開

worker_rlimit_nofile 51200;

events

{

# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];

use epoll; #使用網絡IO模型epoll(linux2.6的高性能方式,FreeBSD建議采用kqueue,window下不指定)

worker_connections 51200; #每個進程最大連接數(最大連接=連接數x進程數)

}

http

{

#文件擴展名與文件類型映射表

include 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”‘;

log_format download ‘$remote_addr – $remote_user [$time_local] ‘

‘”$request” $status $bytes_sent ‘

‘”$http_referer” “$http_user_agent” ‘

‘”$http_range” “$sent_http_content_range”‘;

#默認編碼

charset gb2312,utf-8;

server_names_hash_bucket_size 128;

#開啟高效文件傳輸模式

sendfile on;

#以下兩個選項用於防止網絡阻塞 參考http://i.cn.yahoo.com/nesta2001zhang/blog/p_104/

tcp_nopush on;

tcp_nodelay on;

#長鏈接超時時間

keepalive_timeout 300;

#fastcgi連接超時時間,下面的看字面意思都能理解個大概了,就不解釋了.

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 128k;

fastcgi_buffers 4 256k;

fastcgi_busy_buffers_size 256k;

fastcgi_temp_file_write_size 256k;

fastcgi_temp_path /dev/shm;

#打開gzip壓縮

gzip on;

#最小壓縮文件大小

gzip_min_length 1k;

#壓縮緩沖區

gzip_buffers 4 8k;

#壓縮版本(默認1.1,前端為squid2.5使用1.0)

gzip_http_version 1.1;

#壓縮類型,默認就已經包含text/html 所以下面就不用再寫了,當然寫上去的話,也不會有問題,但是會有一個warn

gzip_types text/plain application/x-javascript text/css text/html text/javascript application/xml;

#錯誤頁面

error_page 404 http://www.g.cn;

error_page 403 http://www.g.cn;

#上傳文件大小限制

client_max_body_size 20m;

#設定請求緩

client_header_buffer_size 16k;

large_client_header_buffers 4 64k;

#設定負載均衡的服務器列表

#如果在同一台機器上,單獨起4組獨立的php-cgi進程(每組8個子進程),性能應該不如1組php-cgi進程(32個子進程),因為1組進程,eaccelerator的PHP二進制文件緩存是共享的,1組進程命中率較高。

#不過好處是,碰到某組的php假死的話,其他端口就可以接管了,我實測下來似乎發生502錯誤的概率降低了很多,或者說我這樣配置以後還沒有遇到

upstream mysvr {

#weigth參數表示權值,權值越高被分配到的幾率越大

#本機上的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

{

listen 80;

server_name www.52crack.com;

index index.html Index.html index.htm index.php;

root /var/htdocs/52crack;

if (-d $request_filename)

{

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

#設定本虛擬主機的訪問日志

access_log logs/www.52crack.com.access.log main;

location ~ .*\.php?$

{

include fcgi.conf;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

}

#如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid

#如果這些文件較多,不推薦這種方式,因為通過squid的緩存效果更好

location ~ ^/(img|js|css)/ {

root /var/htdocs/52crack;

expires 24h;

}

#對 “/” 啟用負載均衡

location / {

proxy_pass http://127.0.0.1;

proxy_redirect off;

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;

client_body_buffer_size 128k;

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffe#使用哪個用戶啟動nginx 前面是用戶,後面是組

user www www;

#nginx工作的進程數量(通常等於CPU數量或者2倍於CPU)

worker_processes 2;

# [ debug | info | notice | warn | error | crit ] 錯誤日志的位置

error_log /var/htdocs/logs/nginx_error.log crit;

Copyright © Linux教程網 All Rights Reserved