最近幾個月的項目都是在nginx 環境下搭建的,特此記錄下設定檔的相關內容,以備不時之需,歡迎吐槽~~
詳細配置資訊請勤奮查閱官方文檔:http://wiki.nginx.org/Configuration
#指定Nginx啟動並執行使用者和使用者組,據此可設定nginx訪問檔案夾的許可權,防止非法使用者訪問無許可權檔案夾。順便提一句,對於php工程的記錄檔讀寫
#所屬使用者不是nginx,因為nginx是將php轉寄給php-cgi來解析,因此使用者是php-cgi的所有者,一般是www-data
user nginx nginx;
#開啟nginx的進程數目,數目應小於等於CPU總核心數,提高程式的並發性
worker_processes 4;
#全域訪問和錯誤記錄檔及其層級,[ debug | info | notice | warn | error | crit ]
access_log /var/nginx/access.log;
error_log /var/nginx/error.log warn;
#預設的日誌格式設定
log_format access $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#進程運行時的檔案
pid /run/nginx.pid;
#工作模式與串連數上限
events
{
#事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
# epoll模型是Linux 2.6以上版本核心中的高效能網路I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#單個進程最大串連數(最大串連數=串連數*進程數),我的配置是768*4
worker_connections 768;
#開啟同時接受多個請求,高並發
multi_accept on;
}
#設定http伺服器
http
{
include mime.types; #使用MIME格式,副檔名與檔案類型映射表
default_type application/octet-stream; #預設檔案類型,檔案流
#charset utf-8; #預設編碼
server_names_hash_bucket_size 128; #伺服器名字的hash表大小
#設定上傳檔案的大小上限為8MB
client_max_body_size 8m;
#開啟高效檔案傳輸模式,調用 sendfile 函數(zero copy 方式)來輸出檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,#可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載和uptime。注意:如果圖片顯示不正常把這個改成off。
sendfile on;
#設定訪問黑白名單, 檔案名稱,大小
white_black_list_conf conf/white.list zone=white1:4m;
white_black_list_conf conf/black.list zone=black1:4m;
white_list white1 on; #白名單 white1 在整個http{} 中都開啟
black_list black1 on; #黑名單 black1 在整個http{} 中都開啟
tcp_nopush on; #防止網路阻塞
tcp_nodelay on; #防止網路阻塞
keepalive_timeout 65; #長連線逾時時間,單位是秒
#開啟gzip,GNU 壓縮靜態資源,最佳化網站訪問速度
gzip on; #開啟gzip壓縮輸出
gzip_min_length 1k; #開啟壓縮的檔案大小下限
gzip_buffers 4 16k; #設定系統擷取幾個單位的緩衝用於儲存gzip的壓縮結果資料流,一次申請4*16=64k緩衝
gzip_http_version 1.0; #壓縮使用http1.0及以上的檔案
gzip_comp_level 2; #壓縮等級,1-10,越大壓縮率越高
#壓縮檔類型,css,js,php,jpg,png
gzip_typestext/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png
gzip_vary on;#允許例外
gzip_disable"MISE[1-6]"#IE6及以下禁用壓縮
#limit_zone crawler $binary_remote_addr 10m; #開啟限制IP串連數的時候需要使用
upstream www.xxx.com {
#upstream的負載平衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
#FastCGI相關參數是為了改善網站的效能:減少資源佔用,提高訪問速度。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#各伺服器的配置
server
{
#預設監聽http 80連接埠, both ipv4 & ipv6
listen 80;
listen [::]:80 default_server ipv6only=on;
#網域名稱可以有多個,用空格隔開
server_name www.xx.com xx.com;
index index.php;
root /data/www/xx;
#手機端導向另一個頁面
location / {
#判斷訪問終端類型
if ($http_user_agent ~* '(Android|iPhone|iPad|webOS|iPod|BlackBerry)') {
rewrite ^.+ http://m.xx.com;
}
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
#靜態檔案直接存取
#圖片緩衝時間設定, 類似於html頁面meta標籤,可設定expire or max-age
location ~ \.(html|htm|gif|jpg|jpeg|png|bmp|swf)$ {
expires 10d;
root /usr/share/nginx/html;
}
#php導向9000 連接埠
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#JS和CSS緩衝時間設定
location ~ .*.(js|css)?$
{
expires 1h;
}
#錯誤頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#對 "/" 啟用反向 Proxy
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web伺服器可以通過X-Forwarded-For擷取使用者真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向 Proxy的配置,可選。
proxy_set_header Host $host;
client_max_body_size 10m; #允許用戶端請求的最大單檔案位元組數
client_body_buffer_size 128k; #緩衝區代理緩衝使用者端請求的最大位元組數,
proxy_connect_timeout 90; #nginx跟後端伺服器連線逾時時間(代理連線逾時)
proxy_send_timeout 90; #後端伺服器資料回傳時間(代理髮送逾時)
proxy_read_timeout 90; #串連成功後,後端伺服器回應時間(代理接收逾時)
proxy_buffer_size 4k; #設定Proxy 伺服器(nginx)儲存使用者頭資訊的緩衝區大小
proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k以下的設定
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#設定快取檔案夾大小,大於這個值,將從upstream伺服器傳
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
#htpasswd檔案的內容可以用apache提供的htpasswd工具來產生。
}
#如果使用j2ee, 可將頁面導向8080連接埠,所有jsp的頁面均交由tomcat或resin處理
location ~ \.(jsp|jspx|do|action)?$ {
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;
}
}
#再設定一個移動端訪問的server, 接過手機端訪問主站導來的請求
server {
listen 80;
server_name m.xx.com www.m.xx.com;
access_log /usr/share/nginx/logs/mobile_access.log;
error_log /usr/share/nginx/logs/mobile_error.log;
location / {
root /usr/share/nginx/html/mobile;
index index.html index.htm;
}
location ~ \.(html|htm|gif|jpg|png)$ {
root /usr/share/nginx/html/mobile;
}
location ~ \.php$ {
root /usr/share/nginx/html/mobile;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
以上就介紹了Nginx.conf 配置小結,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。