Nginx Complete Configuration File Example

Source: Internet
Author: User
Tags epoll php file php script sendfile tomcat

Example one:

This is a more complete example of nginx configuration file, the following nginx.conf simple implementation of nginx in the front-end to do reverse proxy server example, processing JS, PNG and other static files, JSP and other dynamic requests forwarded to other server Tomcat, as well as load-balanced configuration.

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 {include mime.types;

    Default_type Application/octet-stream; #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sen

    T "$http _referer" ' # ' "$http _user_agent" "$http _x_forwarded_for";

    #access_log Logs/access.log Main;
    Sendfile on;

    # Tcp_nopush on;

  Keepalive_timeout 65;
    # gzip compression feature set 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 set 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; 
              # Set Load Balancer background server list 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;
        } # Very important virtual host Configuration server {Listen 80;
        server_name itoatest.example.com;

        Root/apps/oaapp;
        CharSet Utf-8;

        Access_log Logs/host.access.log Main;
            #对/all do load balancer + Reverse proxy location/{Root/apps/oaapp;

            Index index.jsp index.html index.htm;  
            Proxy_pass Http://backend;
            Proxy_redirect off; # Backend Web server can get the user real IP through x-forwarded-for 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 own processing, do not go to backend request 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 502 503 504/50x.html;
     Location =/50x.html {root html;   }} # # Other virtual host, server instruction started} 

Example two:

#运行用户 user nobody;

#启动进程, usually set to equal to the number of CPUs worker_processes 1;
#全局错误日志及PID文件 #error_log Logs/error.log;
#error_log Logs/error.log Notice;

#error_log Logs/error.log Info;

#pid Logs/nginx.pid; 

    #工作模式及连接数上限 Events {#epoll是多路复用IO (I/O multiplexing), #仅用于linux2.6 above the kernel, can greatly improve the performance of nginx use Epoll;

    Maximum number of concurrent links for #单个后台worker process processes worker_connections 1024; # Concurrent totals are the product of worker_processes and worker_connections # i.e. max_clients = worker_processes * Worker_connections # in the reverse setting  Agent case, max_clients = worker_processes * WORKER_CONNECTIONS/4 Why # Why is the reverse proxy above divided by 4, should be said to be an empirical value # according to the above conditions, the nginx under normal circumstances
    The maximum number of connections that the server can handle is: 4 * 8000 = 32000 # The setting of the Worker_connections value is related to the physical memory size # Because of concurrent IO constraints, the value of max_clients must be less than the maximum number of files that the system can open # while the maximum number of files that the system can open is proportional to the size of the memory, the number of files that can be opened on the 1GB memory machine is about 100,000 # We'll look at the number of file handles that the VPS of 360M memory can open: # $ cat/proc/sys/fs/file-ma X # output 34336 # 32000 < 34336, that is, the total number of concurrent connections is less than the total number of file handles the system can open, so that the operating system can withstand the range # Therefore, the value of Worker_connections is based on WThe number of orker_processes processes and the maximum number of files that the system can open are set appropriately # so that the total number of concurrent sessions is less than the maximum number of files that the operating system can open # Its essence is to configure the host's physical CPU and memory # Of course, the total number of concurrent sums in theory can
    Can deviate from the actual situation, because the host has other work processes that need to consume system resources.
    # Ulimit-shn 65535} http {#设定mime类型, type defined by Mime.type file 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 instruction Specifies whether Nginx calls the Sendfile function (zero copy mode) to output the file, #对于普通应用 must be set to ON, #如果用来进行下载等应用磁盘IO重负载应用, can be set to OFF, #以平
    Disk and network I/O processing speed, reducing the uptime of the system.
    Sendfile on;

    #tcp_nopush on;
    #连接超时时间 #keepalive_timeout 0;
    Keepalive_timeout 65;

    Tcp_nodelay on;
    #开启gzip压缩 gzip on;

    Gzip_disable "MSIE [1-6].";
    #设定请求缓冲 client_header_buffer_size 128k; Large_client_header_buffers 4 128k;


    #设定虚拟主机配置 server {#侦听80端口 Listen 80;

        #定义使用 www.nginx.cn access to server_name www.nginx.cn;

        #定义服务器的默认网站根目录位置 root html;

        #设定本虚拟主机的访问日志 Access_log Logs/nginx.access.log Main;   

        #默认请求 location/{#定义首页索引文件的名称 index index.php index.html index.htm;
        } # Definition error page error_page 502 503 504/50x.html; Location =/50x.html {} #静态文件, Nginx handles location ~ ^/(Images|javascript|js|css|flash|media|stati
            c)/{#过期30天, the static file is not updated, the expiration can be set a little larger, #如果频繁更新, you can set a smaller point.
        Expires 30d; #PHP script requests are forwarded to fastcgi processing.
        Use the fastcgi default configuration.
            Location ~. php$ {Fastcgi_pass 127.0.0.1:9000;
            Fastcgi_index index.php;
            Fastcgi_param script_filename $document _root$fastcgi_script_name;
        Include Fastcgi_params;
         #禁止访问. htxxx file   Location ~/.ht {deny all; }

    }
}

Example three:

User www www; #指定Nginx worker process run user and user group worker_processes 2; #指定了Nginx要开启的进程数, multi-core CPUs specify as many processes as the number of cores PID logs/nginx.pid; #指定进程id的存储文件位置 Worker_rlimit_nofile 65535;  #指定单进程打开文件数, the system must be set in accordance with events {use Epoll; #指定nginx工作模式, Nginx main working mode has SELECT, poll, Kqueue, Epoll wherein Select, poll is the standard mode of operation, Kqueue, epoll for efficient operation mode, Epoll used in the Linux system, and Kqueue used in BSD system worker_connections 65535; #指定单进程的最大连接数} HTTP part http {include mime.types; #指定配置文件所 Contains files Default_type application/octet-stream;  #指定默认类型为二进制流, that is, when the file type is not defined in this way, for example, when the PHP environment is not configured, Nginx is not resolved, at this time, the browser to access the PHP file will appear in the Download window Log_format main ' $remote _addr-

                      $remote _user [$time _local] "$request" ' #设定日志格式 ' $status $body _bytes_sent "$http _referer" '

   

 

' "$http _user_agent" "$http _x_forwarded_for" ';;  Client_max_body_size 20m;

    #设置允许客户端请求的最大的单个文件字节数 client_header_buffer_size 16k; #指定来自客户端请求头的headerbuffer大小, if you customize the message header or have a larger cookie, you can increase the buffer size here Large_client_headeR_buffers 4 32k; #指定客户端请求中较大的消息头的缓存最大数量和大小, 4 is the number, 32k is the size, the maximum cache is 4 32kb sendfile on; #开启高效传输模式 Tcp_nopush  On         

   

    # Tcp_nopush,tcp_nodelay set on to prevent network blocking tcp_nodelay on; Keepalive_timeout 65;

    #指定客户端连接保持活动的超时时间 client_header_timeout; #指定客户端请求头读取超时时间, if the client has not sent any data over this time, Nginx will return "Request Time Out (408)" Error        Client_body_timeout; #指定客户端请求主体读取超时时间, if the client has not sent any data over this time, Nginx will return "Request Time Out (408)" Error send_timeout #指定响应客户端的超时时间. This timeout is limited to two connection activities between the time, if more than this time, the client does not have any activity, Nginx will close the connection gzip on; #开启gzip压缩, the real-time compressed output data stream Gzip_min_length 1k; #设置允许压缩的页面最小字节数 gzip_buffers 4 16k; #指定内存空间来存贮压缩结果, this specifies 4 units of 16k of memory to store the compression result, that is, the total size is 64k gzip_http_version 1.1; #指定识别HTTP协议版本, the default is 1.1 Gzip_comp_level 2; #指 Fixed gzip compression ratio, 1 compression ratio is minimum, processing speed is the fastest, 9 compression ratio is maximum, transmission speed, but processing slowest, also consumes CPU resources gzip_types Text/plain Application/x-javascript text/css appli Cation/xml; #指定压缩的类型, whether or not specified, the "text/html" type is always compressed gzip_vary on; #该选项开启可以让前端的缓存服务器缓存经过gzip压缩的页面, for example, withVarnish cache data Server_tokens off with nginx compression, #隐藏Nginx版本号 server {Listen 8000; #指定Nginx监端口 serve R_name localhost; #用来指定IP或者域名 charset utf-8; #指定Nginx默认的字符集, only utf-8 supports Chinese characters access_log logs/host.access . log main; #指定访问日志的名称及位置 location/{index index.html index.htm; #设定默认首页 root/tom/ webapps/root; #指定网页根目录} location ~ (jsp|\?)

        {#指定url中包含jsp或者 All forwards to 192.168.0.10 's 80 port that Tomcat handles Proxy_pass http://192.168.0.10:80; }

Supplementary reading: http://blog.csdn.net/field_yang/article/details/52278390


Status Monitoring module:



Note The URL of the visit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.