Nginx安装与配置

什么是Nginx?

Nginx (engine x) 是一个高性能的HTTP、反向代理服务器。——摘自百度百科。

如何安装?

安装编译所需类库

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

下载nginx源码

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

解压

tar zxvf nginx-1.6.3.tar.gz

配置编译模块

cd nginx-1.6.3
./configure --prefix=/opt/nginx-1.6.3 --with-pcre \
--with-http_stub_status_module --with-http_ssl_module \
--with-http_gzip_static_module --with-http_realip_module

如果需要用到其他模块,自行根据所需添加即可

编译并安装

make && make install

nginx配置文件样例

#common setting
user  xxx;
worker_processes  8;
pid     /opt/nginx-1.6.3/nginx.pid;
error_log /data/logs/nginx/error.log;

events {
    use epoll;
    worker_connections  10240;
}


http {
    include             mime.types;
    default_type        application/octet-stream;
    log_format          main    '$remote_addr $request_length $request_time [$time_local] "$request" $status $bytes_sent '
                                '$http_host "$proxy_add_x_forwarded_for" "$http_referer" $upstream_addr $upstream_response_time';
    access_log                  /data/logs/nginx/access_log main;
    server_tokens               off;
    gzip                        on;
    gzip_proxied                any;
    gzip_vary                   on;
    gzip_min_length             1000;
    gzip_types                  application/x-javascript text/plain application/xml text/xml application/xhtml+xml text/css text/javascript;
    sendfile                    on;
    port_in_redirect            on;
    keepalive_timeout           60;
    keepalive_requests          1000;
    log_not_found               on;
    client_max_body_size        50M;
    client_header_buffer_size   16k;
    large_client_header_buffers 8 32k;
    client_body_timeout         300;
    client_body_buffer_size     3072k;

    
    upstream xxx-api-server {
            server  172.10.111.20:8080;
            server  172.10.111.20:8082 backup;
    }
        
server {
        listen 80 ;
        server_name www.test.com;
        root       /opt/nginx-1.6.3/html/;
        error_page 404 502 = @fetch;
        location  @fetch {
                 default_type application/json;
                 return 200 '{"eorror":10000,"msg":"系统优化维护中..."}';
        }
        location ~ / {
                proxy_pass http://xxx-api-server;
                proxy_read_timeout      7200;
                proxy_connect_timeout   5;
                proxy_set_header        Host                $Host;
                proxy_set_header        X-Forwarded-For     $remote_addr;
        }
    }
server {
        listen 443 ;
        server_name www.test.com;
        ssl on;
        ssl_certificate /opt/nginx-1.6.3/certs/test.pem;
        ssl_certificate_key /opt/nginx-1.6.3/certs/test.key;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        root    /opt/nginx-1.6.3/html/;
        error_page 404 502 = @fetch;
        location  @fetch {
                 default_type application/json;
                 return 200 '{"eorror":10000,"msg":"系统优化维护中..."}';
        }
        location ~ / {
                proxy_pass http://xxx-api-server;
                proxy_read_timeout      7200;
                proxy_connect_timeout   5;
                proxy_set_header        Host                $Host;
                proxy_set_header        X-Forwarded-For     $remote_addr;
        }
    }
server {
        listen 80 ;
        server_name www.test-php.com;
        error_page 404 502 = @fetch;
        location  @fetch {
           default_type application/json;
           return 200 '{"eorror":10000,"msg":"系统优化维护中..."}';
        }
        location  ~ \.(js|css|gif|jpg|jpeg|png|svg|woff2|woff|ttf)$ {
                root /opt/nginx-1.6.3/html/php/;
        }
        location ~ / {
                root /opt/nginx-1.6.3/html/php/;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                if (-f $request_filename/index.html){
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename/index.php){
                        rewrite (.*) $1/index.php;
                }
                if (!-f $request_filename){
                        rewrite (.*) /index.php;
                }
        }
    }
}

你可能感兴趣的:(Nginx安装与配置)