开源 nginx wordpress mysql shell Android 编程 Ubuntu linux Windows php 云计算 Firefox centos apache Python java 微软 程序员 google

nginx.conf文件实例(完成tomcat,php集成,pathinfo,SSI,gzip,动静分离)

#Main

user www www;
worker_processes 8;
error_log  /data/logs/Nginx/nginx_error.log  crit;
pid        /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

#HTTP

http
{
  include       mime.types;
  default_type  application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 20m;

  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  ssi on;
  ssi_silent_errors on;
  ssi_types text/shtml;
  log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';

#Upstream
upstream web {
             server 127.0.0.1:8080 weight=5 max_fails=2 fail_timeout=120s;
             }

#Server

server
     {
            listen         192.168.1.18:80;
            server_name    wxd5981.com www.wxd5981.com;
            index index.jsp index.html;
            root /data/wwwroot/wxd5981/;
            error_page 404  http://www.wxd5981.com/404.html;

#Location

    location ~ .*.(jsp|action|do)$
    {
       index index.jsp;
       proxy_redirect off ;
       proxy_set_header host $host;
       proxy_set_header X-Real-ip $remote_addr;
       proxy_set_header REMOTE-HOST $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       client_max_body_size 20m;
       client_body_buffer_size 256k;
       proxy_connect_timeout 300;
       proxy_send_timeout 300;
       proxy_read_timeout 300;
       proxy_buffer_size 64k;
       proxy_buffers 8 512k;
       proxy_busy_buffers_size 1m;
       proxy_temp_file_write_size 2m;
       proxy_max_temp_file_size 128m;
       proxy_pass http://web;
    }

    location ~ .*.(PHP|php5)
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      set $path_info "";
      set $real_script_name $fastcgi_script_name;
      if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
            set $real_script_name $1;
            set $path_info $2;
      }

      fastcgi_param  SCRIPT_FILENAME $document_root$real_script_name;
      fastcgi_param  SCRIPT_NAME $real_script_name;
      fastcgi_param  PATH_INFO $path_info;
      fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
      fastcgi_param  SERVER_SOFTWARE    nginx;
      fastcgi_param  QUERY_STRING       $query_string;
      fastcgi_param  REQUEST_METHOD     $request_method;
      fastcgi_param  CONTENT_TYPE       $content_type;
      fastcgi_param  CONTENT_LENGTH     $content_length;
      fastcgi_param  REQUEST_URI        $request_uri;
      fastcgi_param  DOCUMENT_URI       $document_uri;
      fastcgi_param  DOCUMENT_ROOT      $document_root;
      fastcgi_param  SERVER_PROTOCOL    $server_protocol;
      fastcgi_param  REMOTE_ADDR        $remote_addr;
      fastcgi_param  REMOTE_PORT        $remote_port;
      fastcgi_param  SERVER_ADDR        $server_addr;
      fastcgi_param  SERVER_PORT        $server_port;
      fastcgi_param  SERVER_NAME        $server_name;
      fastcgi_param  REDIRECT_STATUS    200;
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      24h;
    }

    location ~ .*.(js|css)?$
    {
      expires      24h;
    }

    access_log  /data/logs/nginx/wxd5981.log access;
}

}

知识相关:

1. 下载nginx: http://nginx.org/en/download.html
2. 编译参数: ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
3. 切割nginx日志脚本

#!/bin/bash
logs_path="/data/logs/nginx/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}wxd5981.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/wxd5981_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`

 优化系统参数

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024  65535

延伸阅读

评论