文档分值:0

nginx >>> nginx >>> nginx 基本配置

课程目录

nginx 基础知识
nginx 基本配置
nginx 进阶
nginx的基本配置

配置文件

nginx.conf

典型nginx配置

#用户和组信息        
user        www    www;

#工作进程
worker_processes        2;

#错误日志    
error_log        logs/error.log;
#error_log        logs/error.log        notice;
#error_log        logs/error.log        info;

pid                                logs/nginx.pid;

#模型    
events    {
                use    epoll;
                worker_connections        2048;
}

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

                #log_format        main        '$remote_addr    -    $remote_user    [$time_local]    "$request"    '
                #                                                                        '$status    $body_bytes_sent    "$http_referer"    '
                #                                                                        '"$http_user_agent"    "$http_x_forwarded_for"';

                #access_log        logs/access.log        main;

                sendfile                                on;
                #    tcp_nopush                    on;

                keepalive_timeout        65;

        #    gzip压缩功能设置
                gzip    on;
                gzip_min_length    1k;
                gzip_buffers                4    16k;
                gzip_http_version    1.0;
                gzip_comp_level    6;
                gzip_types    text/html    text/plain    text/css    text/javascript    application/json    application/javascript    application/x-javascript    application/xml;
                gzip_vary    on;

        #    http_proxy    设置
                client_max_body_size            10m;
                client_body_buffer_size            128k;
                proxy_connect_timeout            75;
                proxy_send_timeout            75;
                proxy_read_timeout            75;
                proxy_buffer_size            4k;
                proxy_buffers            4    32k;
                proxy_busy_buffers_size            64k;
                proxy_temp_file_write_size        64k;
                proxy_temp_path            /usr/local/nginx/proxy_temp    1    2;

        #    设定负载均衡后台服务器列表    
                upstream        backend        {    
                                                        #ip_hash;    
                                                        server            192.168.10.100:8080    max_fails=2    fail_timeout=30s    ;        
                                                        server            192.168.10.101:8080    max_fails=2    fail_timeout=30s    ;        
                }

        #    很重要的虚拟主机配置
                server    {
                                listen                            80;   #端口
                                server_name        itoatest.example.com;
                                root            /apps/oaapp;

                                charset    utf-8;
                                access_log        logs/host.access.log        main;

                                #对    /    所有做负载均衡+反向代理
                                location    /    {
                                                root            /apps/oaapp;
                                                index        index.jsp    index.html    index.htm;

                                                proxy_pass                                http://backend;        
                                                proxy_redirect    off;
                                                #    后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                                                proxy_set_header        Host        $host;
                                                proxy_set_header        X-Real-IP        $remote_addr;        
                                                proxy_set_header        X-Forwarded-For        $proxy_add_x_forwarded_for;
                                                proxy_next_upstream    error    timeout    invalid_header    http_500    http_502    http_503    http_504;

                                }

                                #静态文件,nginx自己处理,不去backend请求tomcat
                                location        ~*    /download/    {        
                                                root    /apps/oa/fs;        

                                }
                                location    ~    .*.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$            
                                {            
                                                root    /apps/oaapp;            
                                                expires                        7d;    
                                }
                                location    /nginx_status    {
                                                stub_status    on;
                                                access_log    off;
                                                allow    192.168.10.0/24;
                                                deny    all;
                                }

                                location    ~    ^/(WEB-INF)/    {            
                                                deny    all;            
                                }
                                #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;
                                }
                }

        ##    其它虚拟主机,server    指令开始
}

本机配置:

#    main全局配置
worker_processes        1;
error_log    logs/erros.log;
#pid                                logs/nginx.pid;

events    {
                worker_connections        1024;
}

#    与提供http服务相关的一些配置参数
http    {
                include                            mime.types;
                default_type        application/octet-stream;

                #log_format        main        '$remote_addr    -    $remote_user    [$time_local]    "$request"    '
                #                                                                        '$status    $body_bytes_sent    "$http_referer"    '
                #                                                                        '"$http_user_agent"    "$http_x_forwarded_for"';

                access_log    logs/access.log;

                sendfile                                on;
                #tcp_nopush                    on;

                keepalive_timeout        65;

                #gzip        on;

                #    虚拟主机配置
    server    {

                                listen                        8089;
                                server_name    localhost;

                                #对    /    所有做负载均衡+反向代理
                                location    /    {
                                                                root    /Users/abc/xiuyetang/;
                                                                index    index.html;

                                }
            }

                server    {
                listen                                            8091;
                server_name                    localhost;
                location    /    {
                            root    /Users/abc/project/React/;
                                                index    index.html;

                }

                include    servers/*;
}

重点关注:http 服务中的 server(虚拟主机配置)

主要结构是:

http    {

    ...

    server    {
        #    监听的端口号
        listen                  8091;
        #    服务器名字,默认为    localhost    
        server_name         localhost;

        location    /    {
            #    代码位置
            #    当用root配置的时候,root后面指定的目录是上级目录
            #    并且该上级目录必须含有和location后指定的名称的同名目录,否则404
            #    root末尾的"/"加不加无所谓
            root    /Users/abc/project/React/;
                    #    index字段声明了解析的后缀名的先后顺序
                    index    index.html

        }

        #    404页面跳转到404.html,相对于上面的root目录
                    error_page        404                                                        /404.html;
                    #    403页面跳转到403.html,相对于上面的root目录
                    error_page        403                                                        /403.html;
                    #    50x页面跳转到50x.html
                    error_page            500    502    503    504        /50x.html;
                    location    =    /50x.html    {
                                    root            html;
                    }

    }

    ...

}

测试配置是否有错误

nginx -t #测试配置是否有错误

如果没有错误,则会提示:

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

[ 该条目创建时间:2016-06-25 17:07:37 ]