`

优化的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并发访问量.doc

    为了提高 Nginx 的并发访问量,我们需要优化 Nginx 的配置文件。例如,我们可以修改 Nginx 的进程数量,调整处理请求数量。我们可以使用以下命令来修改 Nginx 配置文件: ``` [root@proxy nginx-1.12.2]# vim /usr/...

    nginx 配置及优化

    **Nginx配置与优化详解** Nginx是一款高性能的HTTP和反向代理服务器,以其轻量级、高效的性能和高并发处理能力而备受青睐。本文将深入探讨Nginx的配置及其优化策略,帮助你更好地理解和提升Nginx的服务性能。 ### ...

    Nginx实现静态网站部署资源

    此外,还可以通过调整`worker_processes`、`keepalive_timeout`等参数来优化Nginx配置。 10. **错误页面定制** 你可以自定义404、500等错误页面,提供更好的用户体验。在`server`块中添加`error_page`指令即可: ...

    nginx配置多个静态资源.docx

    nginx配置多个静态资源 本文将详细介绍nginx配置多个静态资源的知识点,从基本概念到配置实践,涵盖了nginx配置文件的各个组件和指令。 nginx配置文件结构 nginx配置文件主要由以下几个部分组成: * main块:...

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

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

    项目打包运行dist以及nginx配置

    在IT行业中,项目打包运行和Nginx配置是两个关键环节,它们对于应用程序的部署和发布至关重要。这里我们将深入探讨这两个主题。 首先,项目打包运行通常指的是将开发完成的前端或后端应用转换为可部署的形式。对于...

    windows下 php+nginx配置详解

    设置监听地址和端口,与Nginx配置中的`fastcgi_pass`对应: ```ini listen = 127.0.0.1:9000 ``` 启动Nginx和PHP-FPM服务。如果一切配置无误,现在你应该可以通过浏览器访问`http://localhost/`并看到Nginz的欢迎...

    Nginx配置SSL自签名证书的方法

    3. **编辑Nginx配置文件**:找到Nginx的配置文件,通常是`/etc/nginx/nginx.conf`或`/usr/local/nginx/conf/nginx.conf`。添加一个新的`server`块,配置如下: ```nginx server { listen 80; listen 443 ssl; # ...

    linux-一个支持百万用户量的nginx配置

    当我们面临需要支持百万用户量的场景时,优化Nginx配置至关重要。本文将详细介绍如何配置Nginx来应对大规模用户访问。 1. **理解Nginx的工作原理** Nginx采用事件驱动模型,非阻塞I/O,能够高效地处理大量并发连接...

    ubuntu+nginx安装配置应用说明

    在本文中,我们将深入探讨如何在Ubuntu操作系统上安装和配置Nginx服务器,这是一个流行的开源Web服务器,以其高性能和稳定性而闻名。...通过不断学习和实践,你可以进一步优化Nginx配置,提升Web应用的性能和可靠性。

    Vue项目部署Nginx配置文件 SSL

    在部署 Vue 项目时,Nginx 配置文件 `nginx.conf` 的关键设置如下: 1. **基本配置**: - `server` 块:定义一个监听特定端口(通常是80)的服务器实例。 ```nginx server { listen 80; server_name your...

    nginx配置文件优化版本

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

    nginx配置学习

    深入理解Nginx配置文件的解析机制,特别是`ngx_http_block`函数及其相关数据结构的作用,对于高效地管理和优化Nginx配置至关重要。通过对HTTP Block、Server Block以及Location Block的组织方式的剖析,我们不仅能够...

    nginx配置步骤详细

    Nginx 配置是 Web 服务器中的一种重要配置,需要根据实际情况进行调整和优化。以下是 Nginx 配置步骤详细的知识点总结: 一、worker_processes 配置 * worker_processes 指令指定了 Nginx 的 worker 进程数量,...

    windows下nginx配置https以及同一个端口监听多个网站即监听多个虚拟主机

    在Windows环境下,配置Nginx以支持HTTPS及在同一端口监听多个网站,即配置多个虚拟主机,是一项常见的网络服务设置任务。...同时,根据实际需求,还可以对Nginx配置进行更复杂的优化,如负载均衡、缓存等。

    vue打包部署nginx 配置

    本文将深入探讨“Vue打包部署Nginx配置”的相关知识点。 一、Vue.js项目打包 Vue.js项目在开发完成后,需要通过Webpack进行打包。Webpack是一个模块打包工具,它将各种资源(如JavaScript、CSS、图片等)视为模块...

    nginx网页配置工具 v2.4.7-源码.zip

    【Nginx网页配置工具 v2.4.7 源码详解】 Nginx是一款高性能的Web服务器和反向代理服务器,广泛应用于网站部署、负载均衡和静态内容服务等...同时,这也有助于开发者更好地定制和优化Nginx配置,满足特定项目的需求。

    nginx的各项详细配置-超多注释

    **Nginx是一款高性能的HTTP和反向代理服务器,广泛应用于Web服务,具有轻量级、高并发处理能力以及优秀的稳定性。...了解并熟练掌握这些配置,能够帮助我们更好地管理和优化Nginx服务器,提升Web服务性能。

    企业级应用Nginx配置案例

    本文将基于“企业级应用Nginx配置案例”这一主题,深入探讨Nginx的配置知识,以帮助你在实际工作中更好地理解和应用。 首先,Nginx配置文件通常位于`/etc/nginx/nginx.conf`,它由多个块组成,如`http`、`server`和...

    微服务实战(二)nginx配置

    1. **基础配置**:在Nginx配置文件中,我们需要设置监听端口、日志路径等基本信息。例如: ``` server { listen 80; server_name leyoutest.com; # 替换为你的域名或IP access_log /var/log/nginx/leyou-manage...

Global site tag (gtag.js) - Google Analytics