ubuntu配置nginx

轉載來自:http://xubaoguo.iteye.com/blog/1062078


第一步,安裝nginx

apt-get update
apt-get install nginx
即可完成安裝

啓動nginx:
/etc/init.d/nginx start
然後就可以訪問了,http://localhost/ , 一切正常!如果不能訪問,先不要繼續,看看是什麼原因,解決之後再繼續。

第二步,安裝Php和mysql
安裝php和MySQL:
apt-get install php5-cli php5-cgi mysql-server-5.0 php5-mysql

第三步,安裝FastCgi和配置
我們需要/usr/bin/spawn-fcgi這個文件,而它是屬於lighttpd這個包裏面的,所以我們安裝lighttpd然後把它設置爲開機不啓動:

apt-get install lighttpd #我們只要/usr/bin/spawn-fcgi
rcconf #去掉lighttpd開機自啓動--------------------------------------------強烈推薦
修改nginx的配置文件:/etc/nginx/sites-available/default
修改 server_name 192.168.200.100;
修改index的一行修改爲:
index index.php index.html index.htm;

去掉下面部分的註釋並修改爲:
location ~ \.php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;

}


在server{}內定義日誌文件的位置和相應的格式:
access_log /var/log/nginx/localhost_access.log combined;

 

access_log off;//表示關閉

 


重新啓動nginx:
/etc/init.d/nginx stop
/etc/init.d/nginx start


啓動fastcgi php:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

 
以下步驟我直接運行rcconf設置php-cgi爲開機自啓動即可,所以跳過
---------------------------------------爲了讓php-cgi開機自啓動:
cd /etc/init.d
cp nginx php-cgi
vim php-cgi

替換nginx爲php-cgi

並修改相應部分爲:
DAEMON=/usr/bin/spawn-fcgi
DAEMON_OPTS="-a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi"
...
stop)
        echo -n "Stopping $DESC: "
        pkill -9 php-cgi
        echo "$NAME."

-------------------------------------------------

在/var/www/nginx-default/目錄下創建一個文件:  /var/www/nginx-default/index.php
文件內容是:

< ?php phpinfo();?>

然後瀏覽器訪問nginx就可以看到一切正常了

 

------------------------------------------------------------END 安裝成功

 

配置文件目錄 /etc/nginx/    nginx.conf     /sites-available/default

www目錄 /var/www/nginx-default/

 

啓動fastcgi php:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

 

 

日誌文件:

localhost.access.log  /var/log/nginx/localhost.access.log

access.log  /var/log/nginx/access.log

error.log    /var/log/nginx/error.log

 

 

---------------重定向nginx錯誤頁面的方法

error_page 404  /404.html;

這個404.html保證在nginx主目錄下的html目錄中即可,如果需要在出現404錯誤後直接跳轉到另外一個地址,可以直接設置如下:


error_page 404 
http://www.***.net ;


同樣的方式可以定義常見的403、500等錯誤。


特別注意的是404.html文件頁面大小要超過512k,不然會被ie瀏覽器替換爲ie默認的錯誤頁面。

 

 

------------------------------虛擬主機配置

server {
    listen   80;
    server_name  localhost; 
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www/nginx-default; 
        index index.php index.html index.htm;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}


server {
    listen   80;
    server_name  sdsssdf.localhost.com; 
    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www/nginx-default/console; 
        index index.php index.html index.htm;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

 

----------------------監控  

 

location ~ ^/NginxStatus/ { 

stub_status on; #Nginx 狀態監控配置     


 

 

 

這樣通過 http://localhost/NginxStatus/(最後的/不能掉) 監控到 Nginx 的運行信息:

 

Active connections: 1 
server accepts handled requests
 1 1 5 
Reading: 0 Writing: 1 Waiting: 0

 

 

NginxStatus 顯示的內容意思如下:

  • active connections – 當前 Nginx 正處理的活動連接數。
  • server accepts handled requests -- 總共處理了 14553819 個連接 , 成功創建 14553819 次握手 ( 證明中間沒有失敗的 ), 總共處理了 19239266 個請求 ( 平均每次握手處理了 1.3 個數據請求 )。
  • reading -- nginx 讀取到客戶端的 Header 信息數。
  • writing -- nginx 返回給客戶端的 Header 信息數。
  • waiting -- 開啓 keep-alive 的情況下,這個值等於 active - (reading + writing),意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連接。

 

-------------------------------靜態文件處理

通過正則表達式,我們可讓 Nginx 識別出各種靜態文件

 

location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
        root /var/www/nginx-default/html;
        expires 24h;
        }

對於例如圖片、靜態 HTML 文件、js 腳本文件和 css 樣式文件等,我們希望 Nginx 直接處理並返回給瀏覽器,這樣可以大大的加快網頁瀏覽時的速度。因此對於這類文件我們需要通過 root 指令來指定文件的存放路徑,同時因爲這類文件並不常修改,通過 expires 指令來控制其在瀏覽器的緩存,以減少不必要的請求。 expires 指令可以控制 HTTP 應答中的“ Expires ”和“ Cache-Control ”的頭標(起到控制頁面緩存的作用)。您可以使用例如以下的格式來書寫 Expires:

 

 

expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
expires 30m;
expires 24h;
expires 1d;
expires max;
expires off;

 

 

這樣當你輸入http://192.168.200.100/1.html的時候會自動跳轉到var/www/nginx-default/html/1.html

 

例如 images 路徑下的所有請求可以寫爲:

 

 

 

 

location ~ ^/images/ {
    root /opt/webapp/images;
}

 

 

 

 

------------------------動態頁面請求處理[集羣]

Nginx 本身並不支持現在流行的 JSP、ASP、PHP、PERL 等動態頁面,但是它可以通過反向代理將請求發送到後端的服務器,例如 Tomcat、Apache、IIS 等來完成動態頁面的請求處理。前面的配置示例中,我們首先定義了由 Nginx 直接處理的一些靜態文件請求後,其他所有的請求通過 proxy_pass 指令傳送給後端的服務器 (在上述例子中是 Tomcat)。最簡單的 proxy_pass 用法如下:

 

location / {
    proxy_pass        http://localhost:8080;
    proxy_set_header  X-Real-IP  $remote_addr;
}

 

 

這裏我們沒有使用到集羣,而是將請求直接送到運行在 8080 端口的 Tomcat 服務上來完成類似 JSP 和 Servlet 的請求處理。

當頁面的訪問量非常大的時候,往往需要多個應用服務器來共同承擔動態頁面的執行操作,這時我們就需要使用集羣的架構。 Nginx 通過 upstream 指令來定義一個服務器的集羣,最前面那個完整的例子中我們定義了一個名爲 tomcats 的集羣,這個集羣中包括了三臺服務器共 6 個 Tomcat 服務。而 proxy_pass 指令的寫法變成了:

 

 

 

# 集羣中的所有後臺服務器的配置信息
    upstream tomcats { 
     server 192.168.0.11:8080 weight=10; 
     server 192.168.0.11:8081 weight=10; 
     server 192.168.0.12:8080 weight=10; 
     server 192.168.0.12:8081 weight=10; 
     server 192.168.0.13:8080 weight=10; 
     server 192.168.0.13:8081 weight=10; 
    } 
    location / { 
        proxy_pass http://tomcats;# 反向代理
        include proxy.conf; 
        }

 

 

----------------------壓力測試

wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz   
tar zxvf webbench-1.5.tar.gz   
cd webbench-1.5   
make && make install

#webbench -c 100 -t 10 http://192.168.200.100/info.php

參數說明:-c表示併發數,-t表示持續時間(秒)

 

 

root@ubuntu-desktop:/etc/nginx/sites-available# webbench -c 100 -t 10 http://192.168.200.100/info.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.200.100/info.php
100 clients, running 10 sec.

Speed=19032 pages/min, 18074373 bytes/sec.
Requests: 3172 susceed, 0 failed.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章