Home  >  Article  >  Backend Development  >  Complete configuration example of Nginx

Complete configuration example of Nginx

WBOY
WBOYOriginal
2016-07-28 08:28:32952browse

<code><span>#运行用户</span><span>user</span> nobody;
<span>#启动进程,通常设置成和cpu的数量相等</span><span>worker_processes</span><span>1</span>;

<span>#全局错误日志及PID文件</span><span>#error_log  logs/error.log;</span><span>#error_log  logs/error.log  notice;</span><span>#error_log  logs/error.log  info;</span><span>#pid        logs/nginx.pid;</span><span>#工作模式及连接数上限</span><span>events</span> {
    <span>#epoll是多路复用IO(I/O Multiplexing)中的一种方式,</span><span>#仅用于linux2.6以上内核,可以大大提高nginx的性能</span><span>use</span><span>epoll</span>; 

    <span>#单个后台worker process进程的最大并发链接数    </span><span>worker_connections</span><span>1024</span>;

    <span># 并发总数是 worker_processes 和 worker_connections 的乘积</span><span># 即 max_clients = worker_processes * worker_connections</span><span># 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4  为什么</span><span># 为什么上面反向代理要除以4,应该说是一个经验值</span><span># 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000</span><span># worker_connections 值的设置跟物理内存大小有关</span><span># 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数</span><span># 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右</span><span># 我们来看看360M内存的VPS可以打开的文件句柄数是多少:</span><span># $ cat /proc/sys/fs/file-max</span><span># 输出 34336</span><span># 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内</span><span># 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置</span><span># 使得并发总数小于操作系统可以打开的最大文件数目</span><span># 其实质也就是根据主机的物理CPU和内存进行配置</span><span># 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。</span><span># ulimit -SHn 65535</span>}


<span>http</span> {
    <span>#设定mime类型,类型由mime.type文件定义</span><span>include</span>    mime.types;
    <span>default_type</span>  application/octet-stream;
    <span>#设定日志格式</span><span>log_format</span>  main  <span>'<span>$remote_addr</span> - <span>$remote_user</span> [<span>$time_local</span>] "<span>$request</span>" '</span><span>'<span>$status</span><span>$body_bytes_sent</span> "<span>$http_referer</span>" '</span><span>'"<span>$http_user_agent</span>" "<span>$http_x_forwarded_for</span>"'</span>;

    <span>access_log</span>  logs/access.log  main;

    <span>#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,</span><span>#对于普通应用,必须设为 on,</span><span>#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,</span><span>#以平衡磁盘与网络I/O处理速度,降低系统的uptime.</span><span>sendfile</span><span>on</span>;
    <span>#tcp_nopush     on;</span><span>#连接超时时间</span><span>#keepalive_timeout  0;</span><span>keepalive_timeout</span><span>65</span>;
    <span>tcp_nodelay</span><span>on</span>;

    <span>#开启gzip压缩</span><span>gzip</span><span>on</span>;
    <span>gzip_disable</span><span>"MSIE [1-6]."</span>;

    <span>#设定请求缓冲</span><span>client_header_buffer_size</span><span>128k</span>;
    <span>large_client_header_buffers</span><span>4</span><span>128k</span>;


    <span>#设定虚拟主机配置</span><span>server</span> {
        <span>#侦听80端口</span><span>listen</span><span>80</span>;
        <span>#定义使用 www.nginx.cn访问</span><span>server_name</span>  www.nginx.cn;

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

        <span>#设定本虚拟主机的访问日志</span><span>access_log</span>  logs/nginx.access.log  main;

        <span>#默认请求</span><span>location</span> / {

            <span>#定义首页索引文件的名称</span><span>index</span> index.php index.html index.htm;   

        }

        <span># 定义错误提示页面</span><span>error_page</span><span>500</span><span>502</span><span>503</span><span>504</span> /50x.html;
        <span>location</span> = /50x.html {
        }

        <span>#静态文件,nginx自己处理</span><span>location</span><span>~ ^/(images|javascript|js|css|flash|media|static)/</span> {

            <span>#过期30天,静态文件不怎么更新,过期可以设大一点,</span><span>#如果频繁更新,则可以设置得小一点。</span><span>expires</span><span>30d</span>;
        }

        <span>#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.</span><span>location</span><span>~ .php$</span> {
            <span>fastcgi_pass</span><span>127.0.0.1:9000</span>;
            <span>fastcgi_index</span> index.php;
            <span>fastcgi_param</span>  SCRIPT_FILENAME  <span>$document_root</span><span>$fastcgi_script_name</span>;
            <span>include</span> fastcgi_params;
        }

        <span>#禁止访问 .htxxx 文件</span><span>location</span><span>~ /.ht</span> {
            <span>deny</span> all;
        }

    }
}</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the complete configuration example of Nginx, including all aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn