Nginx:

高性能的HTTP Server, 反向代理, 正向代理

[root@station230 ~]# service httpd stop

[root@station230 ~]# chkconfig httpd off

所需的软件:

开发工具,开发库,openssl-devel

部署Nginx

=========================================================================

pcre: 支持正则表达式,地址重写rewrite

# tar xvf pcre-8.10.tar.gz

# cd pcre-8.10

# ./configure && make && make install

nginx:

# useradd www

# tar xvf nginx-1.2.0.tar.gz

# cd nginx-1.2.0

# ./configure \

--user=www \

--group=www \

--prefix=/usr/local/nginx \

--with-http_stub_status_module \

--with-http_sub_module \

--with-http_ssl_module 

# make

# make install

# tree /usr/local/nginx/

/usr/local/nginx/

|-- conf

|   |-- fastcgi.conf

|   |-- fastcgi.conf.default

|   |-- fastcgi_params

|   |-- fastcgi_params.default

|   |-- koi-utf

|   |-- koi-win

|   |-- mime.types

|   |-- mime.types.default

|   |-- nginx.conf主配置文件

|   |-- nginx.conf.default

|   |-- scgi_params

|   |-- scgi_params.default

|   |-- uwsgi_params

|   |-- uwsgi_params.default

|   `-- win-utf

|-- html

|   |-- 50x.html

|   `-- index.html

|-- logs

`-- sbin

    `-- nginx

启动:

# /usr/local/nginx/sbin/nginx

# ps aux |grep nginx

root     17411  0.0  0.0   6060   700 ? Ss   13:47   0:00 nginx: master process /usr/local/nginx/sbin/nginx

www      17412  0.0  0.0   6280  1064 ? S    13:47   0:00 nginx: worker process      

# netstat -tnlp |grep :80

tcp        0      0 0.0.0.0:80      0.0.0.0:*    LISTEN      17411/nginx     

# links -dump 192.168.2.115

         Welcome to nginx!

# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local 开机运行

====基本配置Nginx===

# sed -i '/^ *#/d;/^$/d' /usr/local/nginx/conf/nginx.conf

==/^#/以#开头

==/^ *#/        以零个或多个空格并接一个#开头          a*  有零个或多个a

==/^$/空行

==-i立即修改 

# vim /usr/local/nginx/conf/nginx.conf

worker_processes  10;初始启动的进程数

worker_connections  1024;  最大连接数

server {

        listen       80;监听的端口

        server_name  localhost;站点名

        location / {

            root   html;//root指令指定网站主目录(相对于nginx的安装目录)

            index  index.html index.htm;//默认主页

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

语法检查:

# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载nginx:

[root@station230 ~]# pgrep nginx

17411

17412

[root@station230 ~]# kill -HUP 17411

[root@station230 ~]# pgrep nginx

17411

17806

17807

17808

17809

17810

17811

17812

17813

17814

17815

# rm -rf /usr/local/nginx/html/*  站点默认主目录

# echo "new nginx..." > /usr/local/nginx/html/index.html

# links -dump 192.168.2.115

   new nginx...

虚拟主机

================================================================

基于IP

基于主机名

基于端口

==基于主机名

一、DNS解析:

# ping www.tianyun.com    ==> 192.168.2.115

# ping www.uplooking.com  ==> 192.168.2.115

二、Nginx

# mkdir -p /webroot/{tianyun,uplooking}

[root@station230 ~]# cat /webroot/tianyun/index.html 

www.tianyun.com

[root@station230 ~]# cat /webroot/uplooking/index.html 

www.uplooking.com

# vim /usr/local/nginx/conf/nginx.conf

 server {

        listen       80;

        server_name  www.tianyun.com;

        location / {

            root   /webroot/tianyun;

            index  index.html index.htm index.php;

        }

    }

 server {

        listen       80;

        server_name  www.uplooking.com;

        location / {

            root   /webroot/uplooking;

            index  index.html index.htm index.php;

        }

    }

[root@station230 ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# pgrep nginx

# kill -HUP 17411

# links -dump www.tianyun.com

   www.tianyun.com

# links -dump www.uplooking.com

   www.uplooking.com

==基于IP

# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:23:54:6a:0e:6d brd ff:ff:ff:ff:ff:ff

    inet 192.168.2.115/24 brd 192.168.2.255 scope global eth0

    inet 192.168.2.116/24 brd 192.168.2.255 scope global secondary eth0:1

    inet6 fe80::223:54ff:fe6a:e6d/64 scope link 

       valid_lft forever preferred_lft forever

# ping www.tianyun.com    ==> 192.168.2.115

# ping www.uplooking.com  ==> 192.168.2.116

# mkdir -p /webroot/{tianyun,uplooking}

[root@station230 ~]# cat /webroot/tianyun/index.html 

www.tianyun.com

[root@station230 ~]# cat /webroot/uplooking/index.html 

www.uplooking.com

# vim /usr/local/nginx/conf/nginx.conf

  server {

        listen       192.168.2.115:80;

        server_name  www.tianyun.com;

        location / {

            root   /webroot/tianyun;

            index  index.html index.htm index.php;

        }

    }

    server {

        listen       192.168.2.116:80;

        server_name  www.uplooking.com;

        location / {

            root   /webroot/uplooking;

            index  index.html index.htm index.php;

        }

    }

[root@station230 ~]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# pgrep nginx

# kill -HUP 17411

# links -dump www.tianyun.com

   www.tianyun.com

# links -dump www.uplooking.com

   www.uplooking.com

基于用户的访问控制

=================================================================================

1. 建立口令文件

# htpasswd -cm /usr/local/nginx/passwd user100

# htpasswd -m /usr/local/nginx/passwd user200

# cat /usr/local/nginx/passwd 

user100:$apr1$Tv9En...$yAuvYCUu2UosYExqP5tHa0

user200:$apr1$oVIFx...$5DJWKMEZYuScBXlaxCKfB/

2. # vim /usr/local/nginx/conf/nginx.conf

 server {

        listen       192.168.2.115:80;

        server_name  www.tianyun.com;

        location / {

            root   /webroot/tianyun;

            index  index.html index.htm index.php;

            auth_basic "test.........";

            auth_basic_user_file /usr/local/nginx/passwd;

        }

    }

3. # kill -1 `cat /usr/local/nginx/logs/nginx.pid`

   # links www.tianyun.com