Nginx安裝與配置

一、安裝

1.1     系統環境與軟件版本

操作系統:CentOS release 5.5 (Final)

內核版本:2.6.18-194.el5

其他軟件: GCC

Nginx版本:nginx-1.0.10.tar.gz(下載地址:http://nginx.org/en/download.html)

 

1.2     編譯安裝

cd  /usr/local/src

wget http://nginx.org/download/nginx-1.0.10.tar.gz

tar xzvf nginx-1.0.10.tar.gz

cd  nginx-1.0.10

./configure --prefix=/usr/local/nginx--with-http_stub_status_module #注:參數--with-http_stub_status_module是打開監控模塊

make && make install

 

以上就安裝完nginx了

 

 

二、配置

2.1 修改配置文件

Nginx的配置文件在/usr/local/nginx/conf/nginx.conf

vim /usr/local/nginx/conf/nginx.conf 就可以編輯他了。下面我給出一個簡單的實例:

 

user  www www;

worker_processes 10;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#最大文件描述符
worker_rlimit_nofile  3000;

events 
{
      use epoll;

      worker_connections 3000;
}

http 
{
      include       conf/mime.types;
      default_type  application/octet-stream;

      keepalive_timeout 120;

      tcp_nodelay on;

      upstream  ccc.com  {
              server   192.168.0.2:80;
              server   192.168.0..3:80;
              server   192.168.0.4:80;
              server   192.168.0.5:80;
      }

      upstream  blog.xx.com  {
              server   192.168.0.7:80;
              server   192.168.0.7: 81;
              server   192.168.0.7: 82;
      }

      server
      {
              listen  80;
              server_name  www.xxx.com;

              location / {
                      proxy_pass        http://ccc.com;
                      proxy_set_header   Host            $host;
                      proxy_set_header   X-Real-IP       $remote_addr;
                      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
              }

              log_format  ccc_com '$remote_addr - $remote_user [$time_local] $request '
                               '"$status" $body_bytes_sent"$http_referer" '
                               '"$http_user_agent""$http_x_forwarded_for"';
              access_log  /var/log/ccc.com.log ccc_com;
      }

      server
      {
              listen  80;
              server_name  blog.xx.com;

              location / {
                      proxy_pass        http://blog.xx.com;
                      proxy_set_header   Host            $host;
                      proxy_set_header   X-Real-IP       $remote_addr;
                      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
              }

              log_format  blog_xx_com '$remote_addr - $remote_user [$time_local] $request '
                               '"$status" $body_bytes_sent"$http_referer" '
                               '"$http_user_agent""$http_x_forwarded_for"';
              access_log  /var/log/blog.xx.log blog_xx_com;
      }
}

 

 

2.2建立用戶

因爲我們剛剛配置的運行nginx的用戶是www所以我們要把www用戶建立起來:

groupadd www

useradd –g www www

 

這樣用戶就建立好了

 

2.3 運行測試

/usr/local/nginx/sbin/nginx

如果沒有報錯信息,就啓動了。

查看下啓動進程

ps aux | grep nginx

會看到如下信息:

root     2906  0.0  0.0  4256   624 ?        Ss  15:22   0:00 nginx: master process/usr/local/nginx/sbin/nginx

www      2908  0.0  0.5  1426811072 ?        S    15:22  0:00 nginx: worker process     

www      2909  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2910  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www       2911 0.0  0.5  14268 10836 ?        S   15:22   0:00 nginx: workerprocess     

www      2912  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2913  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2914  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2915  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2916  0.0  0.5 14268 10836 ?        S    15:22  0:00 nginx: worker process     

www      2917  0.0  0.5 14268 10816 ?        S    15:22  0:00 nginx: worker process     

root     2938  0.0  0.0  4176   688 pts/0    R+  15:30   0:00 grep nginx

 

 

說明nginx已經啓動了。接下來就用瀏覽器測試我們剛剛配好的地址。

 

 

三、監控配置

3.1 添加配置

在編譯配置時的參數--with-http_stub_status_module 就是打開監控功能模塊兒,如果沒有加的話,就加上這個參數重新編譯下。

在nginx的配置文件裏找到一個serer配置節,加入下面的配置:

 

location /nginx_status {

stub_status on;

access_log off;

allow192.168.1.1;#設置爲可訪問該狀態信息的ip

deny all;

}

 

3.2 測試

改完配置文件後,可以不停止nginx服務器而只重新加載配置文件。用如下幾個命令都可以:

1) nginx -t;  nginx -s reload
2) nginx -t;  kill -HUP <nginx_master_process_pid>

 

 

打開瀏覽器,輸入地址http://{server配置的地址}/nginx_status就可以查看當前配置下nginx的負載情況了,輸出的內容如下:

Active connections: 20

server accepts handled requests

200 200 286

Reading: 0 Writing: 3 Waiting: 6

解釋:

active connections:nginx 正處理的活動連接數 20個。

server accepts handled requests:nginx啓動到現在共處理了 200個連接 , 成功創建 200 次握手 一般跟第一個一樣,差值爲請求丟失數, 總共處理了286 次請求。

reading :nginx 讀取到客戶端的Header 信息數。

writing : nginx 返回給客戶端的Header 信息數。

waiting :開啓 keep-alive 的情況下,這個值等於active - (reading + writing),意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連接。

這個狀態信息,從nginx啓動算起,包括重載配置文件,也會清零。

 

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