歡迎您光臨本站 註冊首頁

Linux下搭建Nginx環境的搭建

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

一、) 安裝Nginx
1.) 安裝
Nginx發音為[engine x],是由俄羅斯人Igor Sysoev建立的項目,基於BSD許可.據說他當初是F5的成員之一,英文主頁:http://nginx.net.俄羅斯的一些大網站已經使用它超過兩年多了,一直表現不凡.

安裝nginx之前需要安裝pcre包和zlib以支持重寫,正則以及網頁壓縮等等.

安裝pcre

下載地址: http://www.pcre.org/

下載適合自己的版本,然後進行安裝:

tar zxvf pcre-7.7.tar.gz

cd pcre-7.7

make

make install

安裝zlib

下載地址: http://www.zlib.net/

下載適合自己的版本,然後進行安裝:

tar zxvf zlib-1.2.3.tar.gz

cd zlib-1.2.3

make

make install

下載地址: http://www.nginx.net/

等待pcre和zlib安裝完畢,開始安裝nginx

下載適合自己的版本,然後編譯安裝:

Nginx的編譯參數如下:

[root@oracle132 /]# tar zxvf nginx-0.6.31

[root@oracle132 nginx-0.6.31]# cd nginx-0.6.31

特別說明:Nginx需要PCRE模塊的支持,但在RHEL下,即便已經安裝PCRE模塊,Nginx編譯時還是不能正確找到相關庫文件,因此需要做以下變通.

[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre

[root@oracle132 nginx-0.6.31]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a

[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.la

[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/pcre.h

[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre/.libs

[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a

[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la

[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/.libs/pcre.h

上面變通操作完畢,接下來開始編譯安裝.

[root@oracle132 nginx-0.6.31]# ./configure --with-pcre=/usr/include/pcre --with-http_stub_status_module

[root@oracle132 nginx-0.6.31]# vi ./objs/Makefile(註:刪除此文件1006行“./configure --disable-shared”)

[root@oracle132 nginx-0.6.31]#make

[root@oracle132 nginx-0.6.31]#make install

[root@oracle132 nginx-0.6.31]#

[root@oracle132 nginx-0.6.31]#

安裝完畢,默認nginx安裝到了/usr/local/下,進入nginx文件夾,打開配置文件!

[root@oracle132 conf]# pwd

/usr/local/nginx/conf

2)nginx的配置文件詳解

[root@oracle132 conf]# vi nginx.conf

user nobody nobody; #運行用戶

worker_processes 1; #啟動進程

#全局錯誤日誌及PID文件

#error_log logs/error.log;

error_log logs/error.log notice;

#error_log logs/error.log info;

pid logs/nginx.pid;

#工作模式及連接數上限

events {

use epoll;

worker_connections 1024;

}

#設定http伺服器,利用它的反向代理功能提供負載均衡支持

http {

#設定mime類型

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"';

#access_log logs/access.log main;

#設定請求緩衝
client_header_buffer_size 1k;


large_client_header_buffers 4 4k;

#設定access log
sendfile on;

tcp_nopush on;
keepalive_timeout 65;

#啟用網頁壓縮
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;
#設定負載均衡的伺服器列表
upstream ixdba{
#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;
}
#下面是配置虛擬主機
include /usr/local/nginx/conf/proxy.conf;
include /usr/local/nginx/conf/vhosts/www.test.com.conf;

3)虛擬主機配置文件詳解

由於虛擬主機分別有一個文件來指定,下面舉例某個虛擬主機的配置如下:
[root@oracle132 vhosts]#
vi /usr/local/nginx/conf/vhosts/www.test.com.conf;

server {
listen 80; #虛擬主機使用埠
server_name www.test.com; #虛擬主機訪問域名
charset UTF-8; #設定nginx默認字元編碼
#access_log logs/host.access.log main;
#所有jpg格式的圖片都有nginx來處理
location ~ .jpg$ {
root /cicro/cws3/vhosts/www.test.com/ROOT;
expires 30d;
}
#所有gif格式的圖片都有nginx來處理
location ~ .gif$ {
root /cicro/cws3/vhosts/www.test.com/ROOT;
expires 30d;
}
# upload和html下所有文件都有nginx來處理

location ~ ^/(upload|html)/ {

root /cicro/cws3/vhosts/www.test.com/ROOT;

expires 30d;

}

#除去上面的文件,剩下的所有都代理給http://127.0.0.1:8009來訪問

location / {

root /cicro/cws3/vhosts/www.test.com/ROOT;

index index.html;

proxy_pass http://127.0.0.1:8009;

}
#設定查看Nginx狀態的地址

location /NginxStatus {

access_log on;

auth_basic "NginxStatus";

auth_basic_user_file ../htpasswd;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

在上面有設置查看Nginx狀態的地址,需要apache的htpasswd 來生成一個登錄驗證文件,這樣生成一個htpasswd 文件:

[root@oracle132 vhosts]# /usr/local/bin/htpasswd -c htpasswd gaojf

New password: (此處輸入您的密碼)
Re-type new password: (再次輸入您的密碼)
Adding password for user gaojf

上面 /usr/local/bin/htpasswd 是htpasswd 文件的執行路徑,如果沒有這個文件,可以從apache的bin目錄拷貝一個過來即可!

-c是創建一個文件

-c後面的httpasswd是創建驗證文件的名字.

gaojf是創建的用戶

#查看nginxstatus:
http://www.test.com/nginxstatus/,輸入驗證帳號密碼,即可看到類似如下內容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324


第一行表示現在活躍的連接數
第三行的第三個數字錶示Nginx運行到當前時間接受到的總請求數,假如快達到了上限,就需要加大上限值了.

本文出自 「技術成就夢想」 博客,請務必保留此出處http://ixdba.blog.51cto.com/2895551/526468


[火星人 ] Linux下搭建Nginx環境的搭建已經有576次圍觀

http://coctec.com/docs/linux/show-post-48975.html