nginx

来源:互联网 发布:获取json的某个属性 编辑:程序博客网 时间:2024/06/06 03:33

安装说明

系统环境:CentOS-6.3
软件:nginx-1.2.6.tar.gz
安装方式:源码编译安装
安装位置:/usr/local/nginx
下载地址:http://nginx.org/en/download.html

安装前提

在安装nginx前,需要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件。安装必须软件:

[root@admin /]#yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

检查系统安装的Nginx:

[root@admin local]# find -name nginx
./nginx
./nginx/sbin/nginx
./nginx-1.2.6/objs/nginx

卸载原有的Nginx

[root@admin /]# yum remove nginx

安装

将安装包文件上传到/usr/local中执行以下操作:

[root@admin local]# cd /usr/local
[root@admin local]# tar -zxv -f nginx-1.2.6.tar.gz
[root@admin local]# rm -rf nginx-1.2.6.tar.gz
[root@admin local]# mv nginx-1.2.6 nginx

[root@admin local]# cd /usr/local/nginx

[root@admin nginx]# ./configure --prefix=/usr/local/nginx

#上面的安装,默认安装到 /usr/local/nginx 目录下面,你可以指定自己的安装目录

#注意这个 nginx 目录是系统自动生成的,不需要手动新建,它的生成时间是在 make, make install 之后才出现的

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

#上面的安装除了指定安装路径之外还安装了 http_stub_status_module 模块,这个模块用来记录 http请求的日志信息

#因为通常情况下下日志信息会比较大,所以需要把 nginx 安装到磁盘容量比较大的目录下

[root@admin nginx]# make
[root@admin nginx]# make install


安装报错:

时候,我们需要单独安装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
 
make
 
make install

配置

#修改防火墙配置:
[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
[root@admin nginx-1.2.6]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2
[root@admin nginx-1.2.6]# cd /usr/local/nginx/sbin
[root@admin sbin]# ./nginx

停止

#查询nginx主进程号
ps -ef | grep nginx
#停止进程
kill -QUIT 主进程号
#快速停止
kill -TERM 主进程号
#强制停止
pkill -9 nginx

重启

[root@admin local]# /usr/local/nginx/sbin/nginx -s reload

测试

#测试端口
netstat –na|grep 80
#浏览器中测试
http://ip:80


nginx 配置:

1.新建禁止登陆的用户 nginx :useradd user -s /sbin/nologin

2.nginx.cnf 配置文件示例 简单版本:只有一个负载均衡一个 web 服务

####################################################################################

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

}
####################################################################################

3.复杂版本:多个负载均衡,多个 服务,多个端口


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

}

4.由复杂的版本可以看出,nginx 服务在配置的时候,(1)可以指定多个端口。(2)一个负载均衡可以被多个服务名访问。(3)一个端口可以被多个服务监听。(4)一个负载均衡可以配置多个 tomcat 服务。

0 0
原创粉丝点击