nginx配置說明

nginx配置說明
#運行用戶
user  nobody nobody;
#啓動進程
worker_processes  2;
#全局錯誤日誌及PID文件
error_log  logs/error.log notice;
pid        logs/nginx.pid;
#工作模式及連接數上限
events {
use epoll;
worker_connections      1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include      conf/mime.types;
default_type  application/octet-stream;
#設定日誌格式
log_format main        '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#設定請求緩衝
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
#開啓gzip模塊
gzip on;
gzip_min_length  1100;
gzip_buffers    4 8k;
gzip_types      text/plain;
output_buffers  1 32k;
postpone_output  1460;
#設定access log
access_log  logs/access.log  main;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout          3m;
sendfile                on;
tcp_nopush              on;
tcp_nodelay            on;
keepalive_timeout  65;
#設定負載均衡的服務器列表
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    192.168.8.1 www.yejr.com;
charset gb2312;
#設定本虛擬主機的訪問日誌
access_log  logs/www.yejr.com.access.log  main;
#如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid
#如果這些文件較多,不推薦這種方式,因爲通過squid的緩存效果更好
location ~ ^/(img|js|css)/  {
root    /data3/Html;
expires 24h;
}
#對 "/" 啓用負載均衡
location / {
proxy_pass      http://mysvr;
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_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status            on;
access_log              on;
auth_basic              "NginxStatus";
auth_basic_user_file  conf/htpasswd;
}
}
}
備註:conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生即可,內容大致如下:
  3.) 查看 Nginx 運行狀態
輸入地址 http://192.168.8.1/NginxStatus/,輸入驗證帳號密碼,即可看到類似如下內容:
Active connections: 328
server accepts handled requests
9309    8982        28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示目前活躍的連接數
第三行的第三個數字表示Nginx運行到當前時間接受到的總請求數,如果快達到了上限,就需要加大上限值了。
第四行是Nginx的隊列狀態
-----------------------------------------------------------------------------------------------------------------------------
Nginx配置防盜鏈
有個客戶的站點負載過高,於是我們可愛的憤青系統工程師將其的Apache換成Nginx,效果很明顯。現在記錄一些Apache轉換過程中的小細節,留下備忘。
Nginx的防盜鏈
一般的防盜鏈如下:
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.xxx.com www.xxx.net;
if ($invalid_referer) {
rewrite ^/ http://www.xxxcom/403.html;
#return 404;
}
}
第一行:gif|jpg|png|swf|flv
表示對gif、jpg、png、swf、flv後綴的文件實行防盜鏈
第二行:www.xxx.com www.xxx.net
表示對www.xxx.com www.xxx.net這2個來路進行判斷
if{}裏面內容的意思是,如果來路不是指定來路就跳轉到錯誤頁面,當然直接返回404也是可以的。
NginxHttpAccessKeyModule實現防盜鏈
如果不怕麻煩,有條件實現的話,推薦使用NginxHttpAccessKeyModule這個東西。
他的運行方式是:如我的download 目錄下有一個 file.zip 的文件。對應的URI 是http://www.xxx.com/download/file.zip
使用ngx_http_accesskey_module 模塊後http://www.xxx.com/download/file.zip?key=09093abeac094. 只有給定的key值正確了,才能夠下載download目錄下的file.zip。而且 key 值是根據用戶的IP有關的,這樣就可以避免被盜鏈了。
據說NginxHttpAccessKeyModule現在連迅雷都可以防了,可以嘗試一下。
-----------------------------------------------------------------------------------------------------------------------------
Nginx下配置負載均衡
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發佈。儘管還是測試版,但是,Nginx 已經因爲它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了。
首先是配置十分的簡單,而且功能非常強大。真是相見恨晚。
先來看看配置文件怎麼寫吧
worker_processes 1;
events {
worker_connections 1024;
}
http{
upstream myproject {
#這裏指定多個源服務器,ip:端口,80端口的話可寫可不寫
server 192.168.43.158:80;
server 192.168.41.167;
}
server {
listen 8080;
location / {
proxy_pass http://myproject;
}
}
}
nginx的負載均衡有哪些功能呢?
如果後面的服務器其中一臺壞了,它能自動識別,更牛的是它好了之後nginx可以馬上識別
服務器A和B,如果A的響應時間爲3,B的響應時間爲1,那麼nginx會自動調整訪問B的概率是A的3倍,真正做到負載均衡
----------------------------------------------------------------------------------------------------------------------------
在Nginx下支持泛域名解析
在Nginx下支持泛域名解析其實很簡單.只要在在編譯 Nginx的時候加上以下代碼即可
--with-http_sub_module
在配置nginx時:
server {
        # Replace this port with the right one for your requirements
        listen      80;
 #could also be 1.2.3.4:80
 
        # Multiple hostnames seperated by spaces.  Replace these as well.
        server_name  www.yourdomain.com *.yourdomain.com;
        #Alternately: _ *
        root /PATH/TO/WEBROOT/$host;
        error_page  404              http://yourdomain.com/errors/404.html;
        access_log  logs/yourdomain.com.access.log;
        location / {
            root  /PATH/TO/WEBROOT/$host/;
            index  index.php;
        }
 
        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log        off;
            expires          30d;
        }
 
        location ~ .php$ {  
          # By all means use a different server for the fcgi processes if you need to  
          fastcgi_pass  127.0.0.1:YOURFCGIPORTHERE;
          fastcgi_index  index.php;
 
          fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
          fastcgi_param  QUERY_STRING    $query_string;
          fastcgi_param  REQUEST_METHOD  $request_method;
          fastcgi_param  CONTENT_TYPE    $content_type;
          fastcgi_param  CONTENT_LENGTH  $content_length;
          fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
          fastcgi_param  REQUEST_URI        $request_uri;
          fastcgi_param  DOCUMENT_URI      $document_uri;
          fastcgi_param  DOCUMENT_ROOT      $document_root;
          fastcgi_param  SERVER_PROTOCOL    $server_protocol;
          fastcgi_param  REMOTE_ADDR        $remote_addr;
          fastcgi_param  REMOTE_PORT        $remote_port;
          fastcgi_param  SERVER_ADDR        $server_addr;
          fastcgi_param  SERVER_PORT        $server_port;
          fastcgi_param  SERVER_NAME        $server_name;
          fastcgi_intercept_errors on;
        }
 
        location ~ /\.ht {
            deny  all;
        }
    }
然後啓動nginx就可以實現泛域名解析了
-----------------------------------------------------------------------------------------------------------------------------
nginx的虛擬主機
安裝好了Nginx,下面就要考慮詳細配置了。我需要把我原來運行在apache上的若干個虛擬主機以及ssl站點都遷移到Nginx上來。
在這之前,你可能需要調整你的nginx.conf這個配置文件,修改一下里面的一些必備參數,比如nginx的運行用戶等等。
我沒有使用默認的配置,而是自己重新寫了一份,僅供參考:

user httpd httpd;
 
worker_processes 10;
 
pid /usr/local/nginx/nginx.pid;
 
worker_rlimit_nofile 51200;
 
events
{
 use epoll;
 worker_connections 51200;
}
 
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'
 tcp_nopush on;
 tcp_nodelay off;
 
 keepalive_timeout 60;
 client_header_timeout 3m;
 client_body_timeout 3m;
 send_timeout 3m;
 connection_pool_size 256;
 client_header_buffer_size 1k;
 large_client_header_buffers 4 2k;
 request_pool_size 4k;
 output_buffers 4 32k;
 postpone_output 1460;
 client_max_body_size 10m;
 client_body_buffer_size 256k;
 client_body_temp_path /dev/shm/client_body_temp;
 proxy_temp_path /usr/local/nginx/proxy_temp;
 fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
 
 gzip on;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_proxied any;
 gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
 gzip_min_length 1100;
 gzip_buffers 4 8k;
 
 #和apache類似,Nginx也可以使用include指令包含一系列的配置文件,我將虛擬主機的配置統一放在了
 #/usr/local/nginx/conf/vhosts目錄下
 include vhosts/*.conf;
 
 error_log /usr/local/nginx/logs/error.log;
 access_log /usr/local/logs/access.log combined;
}
在/usr/local/nginx/conf目錄下新建文件php_fcgi.conf,保存php的fastcgi設置。我的配置文件如下:
fastcgi_pass  127.0.0.1:19000;
fastcgi_index index.php;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
#new ac upload
#fastcgi_pass_request_body off;
#client_body_in_file_only clean;
#fastcgi_param  REQUEST_BODY_FILE  $request_body_file;
#
 
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
 
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
這裏注意第一行fastcgi_pass,裏面的ip地址以及端口要與在前面配置的spawn-fcgi裏面指定的ip與端口一致,否則Nginx無法將php的請求正確的傳遞到php的fastcgi守護進程。
配置好了php的fastcgi以後,下面可以進行每一個虛擬主機的配置了,以我的ipbfans.org爲例:
server
{
 listen 80;
 server_name www.ipbfans.org ipbfans.org doc.ipbfans.org from1979.cn www.from1979.cn;
 
 index index.php index.html index.htm;
 root /usr/local/nginx/www;
 
 location ~ .*\.php?$
 {
  include php_fcgi.conf;
 }
 
 access_log /usr/local/nginx/logs/ipbfans.org/access.log combined;
 error_log /usr/local/nginx/logs/ipbfans.org/error.log; 
}
如果有多個php的虛擬主機,參照這個配置文件,修改root選項,以及log的保存目錄就可以了。
-----------------------------------------------------------------------------------------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章