`

优化的nginx配置

阅读更多

http://brainspl.at/articles/2007/01/03/new-nginx-conf-with-optimizations

 

 

 

# user and group to run as
user  ez ez;

# number of nginx workers
worker_processes  6;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
  worker_connections 1024;
}

# start the http module where we config http access.
http {
  # pull in mime-types. You can break out your config
  # into as many include's as you want to make it cleaner
  include /etc/nginx/mime.types;

  # set a default type for the rare situation that
  # nothing matches from the mimie-type include
  default_type  application/octet-stream;

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

  # main access log
  access_log  /var/log/nginx_access.log  main;

  # main error log
  error_log  /var/log/nginx_error.log debug;

  # no sendfile on OSX
  sendfile on;

  # These are good default values.
  tcp_nopush        on;
  tcp_nodelay       off;
  # output compression saves bandwidth
  gzip            on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml
application/xml+rss text/javascript;


  # this is where you define your mongrel clusters.
  # you need one of these blocks for each cluster
  # and each one needs its own name to refer to it later.
  upstream mongrel {
    server 127.0.0.1:5000;
    server 127.0.0.1:5001;
    server 127.0.0.1:5002;
  }


  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;
   
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /data/ez/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
     
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) {
        break;
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /data/ez/current/public;
    }
  }

  # This server is setup for ssl. Uncomment if
  # you are using ssl as well as port 80.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 443;
   
    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # sets the domain[s] that this vhost server requests for
    # server_name www.[engineyard].com [engineyard].com;

    # doc root
    root /data/ez/current/public;

    # vhost specific access log
    access_log  /var/log/nginx.vhost.access.log  main;

    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
    }

    location / {
      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;

      # needed for HTTPS
      proxy_set_header X_FORWARDED_PROTO https;

      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
     
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      if (-f $request_filename) {
        break;
      }

      # check for index.html for directory index
      # if its there on the filesystem then rewite
      # the url to add /index.html to the end of it
      # and then break to send it to the next config rules.
      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }

      # this is the meat of the rails page caching config
      # it adds .html to the end of the url and then checks
      # the filesystem for that file. If it exists, then we
      # rewite the url to have explicit .html on the end
      # and then send it on its way to the next config rule.
      # if there is no file on the fs then it sets all the
      # necessary headers and proxies to our upstream mongrels
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }

      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }

    error_page   500 502 503 504  /500.html;
    location = /500.html {
      root   /data/ez/current/public;
    }
  }


}

分享到:
评论

相关推荐

    nginx 配置及优化

    nginx配置优化 每个参数都有详细的解释,包含多线程,负载均衡 ,无效联系我:351137017

    Nginx服务器的安装与配置.pdf

    第3章 Nginx的基本配置与优化.pdf 第4章 Nginx与PHP(FastCGI)的安装、配置与优化.pdf 第5章 Nginx与JSP、ASP.NET、Perl的安装与配置.pdf 第6章 Nginx HTTP负载均衡和反向代理的配置与优化.pdf 第7章 Nginx的Rewrite...

    nginx配置文件优化版本

    整理的nginx的初始化配置文件,做了部分优化,安装nginx后可以直接替换使用。有问题可以直接留言

    Nginx 配置说明文档

    Nginx 配置说明文档,方便管理和配置,在测试工作中很重要,可以优化测试,达到最优配置策略,方便管理,方便运营,方便配置安装。

    nginx优化配置,搭建高性能服务器

    nginx与fastcgi优化相关配置,搭建高性能服务器。

    超详细的nginx配置教程

    超详细的nginx配置教程 nginx作为服务器必备软件,其配置较为复杂,特组织该教程 内容包括: nginx性能优化 大并发 均衡负载 代理模式 限流 动静分享 长连接 文件压缩传输 配置状态监控 数据库 配置nginx支持长连接

    Nginx配置优化手册.docx

    Nginx配置优化手册 1. Nginx优化配置 1 1.1 进程数配置优化 1 1.2 事件模型优化 1 1.3 连接数优化 1 1.4 进程最大打开文件数优化 2 1.5 文件传输优化 2 1.6 超时时间配置 2 1.7 开启GZIP压缩 3 1.8 配置日志异步写入...

    nginx配置优化+负载均衡+动静分离详解.zip_nginx_nginx 负载_优化_优化配置_负载均衡

    nginx配置优化+负载均衡+动静分离详解

    nginx配置优化+负载均衡+动静分离详解

    nginx配置优化+负载均衡+动静分离详解

    nServer-v2.1023[FTP + MYSQL + HTTP + PHP(FCGI)]

    - 优化Nginx配置文件 - 更改目录.default为a.default,解决特定情况无法读取目录的问题 2012年04月05日 - 解决MySQL远程访问慢的问题 2012年03月30日 - 更新Nginx版本为1.0.14 2012年03月20日 - 优化PHP配置 - ...

    nginx基础和优化配置.rar

    nginx基础和优化配置,中小型NGINX的搭建,安全,性能提升等方面的解决方案。

    nginx-配置指南 2018

    第3章 Nginx的基本配置与优化 第4章 Nginx与PHP(FastCGI)的安装、配置与优化 第5章 Nginx与JSP、ASP.NET、Perl的安装与配置 第6章 Nginx HTTP负载均衡和反向代理的配置与优化 第7章 Nginx的Rewrite规则与实例 第8章 ...

    Nginx的基本配置与优化

    Nginx的基本配置与优化,Nginx的基本配置与优化

    Nginx配置优化详解

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能很好地工作了。然而,...

    nginx配置优化和负载均衡

    nginx配置优化+负载均衡+动静分离详解 每个请求按照ip的hash结果分配,同一个ip的访客固定访问一个后端服务器,可解决动态网页session共享问题

    第4章 Nginx与PHP(FastCGI)的安装、配置与优化

    Nginx+FastCGI运行原理 Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了...

    Nginx常用配置、负载均衡及优化

    Nginx常用配置、负载均衡及优化

    详解nginx高并发场景下的优化

    一、这里的优化主要是指对nginx的配置优化,一般来说nginx配置文件中对优化比较有作用的主要有以下几项: 1)nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数。 worker_processes 8; 2)为每...

Global site tag (gtag.js) - Google Analytics