1.卸载已有的 nginx
2.软件下载地址
3.预先安装软件
4.安装 nginx
5.出现问题解决
6.启动和停止nginx
7.停止
8.启动
9.重启
10.测试
11.常见例子->访问静态文件
12.常见例子->一个负载均衡例子
13.常见例子->多个负载均衡,多个服务,多个端口
14.nginx可以实现的效果

  1. 卸载已有的 nginx
find -name nginx
./nginx
./nginx/sbin/nginx
./nginx-1.2.6/objs/nginx
yum remove nginx
  1. 软件下载地址
  2. 预先安装软件
yum install gcc-c++ -y
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
  1. 安装 nginx
cd /usr/local
tar -zxv -f nginx-1.2.6.tar.gz
rm -rf nginx-1.2.6.tar.gz
mv nginx-1.2.6 nginx
cd /usr/local/nginx

#设置安装位置,可以自己定义


#文件夹出现位置是在 make, make install 之后才出现的,添加http检测模块

./configure --prefix=/data/software/nginx --with-http_stub_status_module

#安装

make && make install
  1. 出现问题解决
安装报错:
时候,我们需要单独安装nginx,来处理大量的下载请求。
单独在Centos5安装nginx遇到的rewrite和HTTP cache错误解决办法:
wget http://nginx.org/download/nginx-0.8.33.tar.gz
tar -zxvf nginx-0.8.33.tar.gz 
cd nginx-0.8.33
./configure --prefix=/usr/local/nginx

安装Nginx时报错

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题
yum -y install pcre-devel

错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum -y install openssl openssl-devel

总结:

yum -y install pcre-devel openssl openssl-devel

./configure --prefix=/usr/local/nginx
  1. 配置防火墙
#修改防火墙配置: 

[root@admin nginx-1.2.6]# vi + /etc/sysconfig/iptables

#添加配置项 

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#重启防火墙 

[root@admin nginx-1.2.6]# service iptables restart
  1. 启动和停止nginx
#方法1

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

#方法2

cd /usr/local/nginx/sbin
./nginx
  1. 停止
#查询nginx主进程号 

ps -ef | grep nginx

#停止进程 

kill -QUIT 主进程号 

#快速停止 

kill -TERM 主进程号 

#强制停止 

pkill -9 nginx
  1. 重启
/usr/local/nginx/sbin/nginx -s reload
  1. 测试
#端口测试 

netstat –na|grep 80

#浏览器中测试 

http://ip:80
  1. 常见例子->访问静态文件
user  nginx nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65536;

#error_log  logs/error.log;


#error_log  logs/error.log  notice;


#error_log  logs/error.log  info;



#pid        logs/nginx.pid;



events {
    worker_connections  65536;
}


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

    log_format  main  '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    proxy_ignore_client_abort on;

    keepalive_timeout  65;
    #keepalive_timeout  1000;
    charset utf-8;
    gzip on;
    gzip_min_length 10k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain text/javascript application/javascript application/x-javascript text/css  application/xml application/octet-stream;
    gzip_vary on;

    server {
        #被监听的端口号和网址
        listen       80;
        server_name  www.test.com;

        #charset koi8-r;

        access_log  logs/test_access.log  main;

        location / {
        #这个地方指定被访问的文件夹位置
            root   /data/test;
            index  index.html index.htm;
        }

        #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;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
        root /data/test;
        expires 30d;
        }
    }


    #location ~ /purge(/.*){
    #    allow 192.168.0.0/16;
    #    deny all;
    #    proxy_cache_purge resource $host$1$is_args$args;
    #}
    }
  1. 常见例子->一个负载均衡例子
user  nginx nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65536;

#error_log  logs/error.log;


#error_log  logs/error.log  notice;


#error_log  logs/error.log  info;



#pid        logs/nginx.pid;



events {
    worker_connections  65536;
}


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

# log_format 日志格式

    log_format  main  '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    proxy_ignore_client_abort on;

    keepalive_timeout  65;
    #keepalive_timeout  1000;
    charset utf-8;
    gzip on;
    gzip_min_length 10k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain text/javascript application/javascript application/x-javascript text/css  application/xml application/octet-stream;
    gzip_vary on;


    upstream balance{
        server 192.168.21.77:8080;
    }


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

        #charset koi8-r;

        access_log  logs/website.log  main;
    #request proxy server
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #这里设置代理的位置
            proxy_pass http://balance;
            proxy_redirect default;
        }
        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;#指定全局的 log 是否打开
                allow all;
               # deny all;
        }
    }

}

13. 常见例子->多个负载均衡,多个服务,多个端口

user  nginx nginx;
worker_processes  8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65536;

#error_log  logs/error.log;


#error_log  logs/error.log  notice;


#error_log  logs/error.log  info;



#pid        logs/nginx.pid;



events {
    worker_connections  65536;
}


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

    log_format  main  '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';

    #全局日志记录记录的位置及日志格式
    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    proxy_ignore_client_abort on;

    keepalive_timeout  65;
    #keepalive_timeout  1000;
    charset utf-8;
    gzip on;
    gzip_min_length 10k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain text/javascript application/javascript application/x-javascript text/css  application/xml application/octet-stream;
    gzip_vary on;


#负载均衡示例 1

    upstream balance1{
        server 192.168.21.76:8093 max_fails=3 fail_timeout=30s;
    }

#负载均衡示例 2

    upstream balance2{
        server 192.168.21.76:8070;
        server 192.168.21.76:8071 down;
    }

#负载均衡示例 3

    upstream balance3{
        server 192.168.21.76:8080 max_fails=3 fail_timeout=30s;
    }


#web 服务 1

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

        #charset koi8-r;

#当前 web 服务的日志 位置、格式

        access_log  logs/404_access.log  main;
    #request proxy server
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://balance1;
            proxy_redirect default;
        }
        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;#指定全局的 access_log 是否打开
                allow all;
               # deny all;
        }
    }

    server {
        listen       80;
        server_name website2.com;

        #charset koi8-r;
#
        access_log  logs/website2.log  main;
    #request proxy server
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://balance3;
            proxy_redirect default;
        }
        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;
                allow all;
               # deny all;
        }
    }

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

        #charset koi8-r;

        access_log  logs/website3.log  main;
    #request proxy server
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://balance1;
            proxy_redirect default;
        }
        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;
                allow all;
               # deny all;
        }
    }

    server {
        listen       8060;
        server_name 192.168.1.111;

        #charset koi8-r;

        access_log  logs/website4.log  main;
    #request proxy server
        location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://balance2;
            proxy_redirect default;
        }
        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;
                allow all;
               # deny all;
        }

    }

}
  1. 同一个域名下的不同 url 指向不同应用
worker_processes  1;

#error_log  logs/error.log;


#error_log  logs/error.log  notice;


#error_log  logs/error.log  info;


#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

# log_format 日志格式

    log_format  main  '$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
    log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    proxy_ignore_client_abort on;

    keepalive_timeout  65;
    #keepalive_timeout  1000;
    charset utf-8;
    gzip on;
    gzip_min_length 10k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types text/plain text/javascript application/javascript application/x-javascript text/css  application/xml application/octet-stream;
    gzip_vary on;

    upstream tomcat-cfhd-admin {
        ip_hash;
    server 192.168.87.105:8081;
        server 192.168.87.104:8081;
    }
    upstream tomcat-cfhd-service {
    server 192.168.87.105:8082;
    server 192.168.87.104:8082;
    }

    server {
        listen       80;
        #server_name  203.158.23.152;
        server_name  test.ync365.com;

        #charset koi8-r;

        access_log  logs/hd.access.log  main;
    #request proxy server
    #http://test.ync365.com/cfhd-admin
    #上面的请求将会由 http://tomcat-cfhd-admin 处理
    #****需要注意的是*****
    #http://tomcat-cfhd-admin 所指向的那个tomcat里面所部署的war文件的名称需要固定为
    #cfhd-admin.war
    #因为代理的域名指向了指定了 upstream 下面的 /cfhd-admin,所以那个工程的 war名称必须的跟他一致才能找到
    location /cfhd-admin {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat-cfhd-admin;
        proxy_redirect default;
    client_max_body_size 50m;
        }
    #http://test.ync365.com/cfhd-service
    #上面的请求将会由 http://tomcat-cfhd-service 处理
    #****需要注意的是*****
    #http://tomcat-cfhd-service 所指向的那个tomcat里面所部署的war文件的名称需要固定为
    #cfhd-service.war
    #因为代理的域名指向了指定了 upstream 下面的 /cfhd-service,所以那个工程的 war名称必须的跟他一致才能找到
    location /cfhd-service {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://tomcat-cfhd-service;
        proxy_redirect default;
        }   

        #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;
        }
    location /nginx_status{
                stub_status on;
                access_log off;#指定全局的 log 是否打开
                allow all;
               # deny all;
        }
    }

}
  1. nginx可以实现的效果
    (1)可以指定多个端口
    (2)一个负载均衡可以被多个服务名访问
    (3)一个端口可以被多个服务监听
    (4)一个负载均衡可以配置多个 tomcat 服务