Nginx安装与配置

来源:互联网 发布:中级网络安全工程师 编辑:程序博客网 时间:2024/04/18 20:08

一、安装

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启动算起,包括重载配置文件,也会清零。