Common configuration of Nginx

Source: Internet
Author: User
Tags epoll sendfile

Continuity configuration

Define user and user groups for Nginx run

user www www;

The number of nginx processes, the recommended setting is equal to the total CPU core number.

worker_processes 8;

Global error log definition type, [Debug | info | notice | warn | error | crit]

error_log /var/log/nginx/error.log info;

Process files

pid /var/run/;

The maximum number of file descriptors opened by an nginx process, the theoretical value should be the maximum number of open files (System value ulimit-n) and the number of nginx processes, but the Nginx allocation request is not uniform, so the recommendation is consistent with the value of ulimit-n.

worker_rlimit_nofile 65535;

Maximum number of operating modes and connections

events{#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。use epoll;#单个进程最大连接数(最大连接数=连接数*进程数)worker_connections 65535;}

Setting up an HTTP server

http{include MIME.Types;#文件扩展名与文件类型映射表Default_type Application/Octet-Stream;#默认文件类型#charset utf-8; #默认编码server_names_hash_bucket_size -;#服务器名字的hash表大小client_header_buffer_size32k;#上传文件大小限制large_client_header_buffers464k;#设定请求缓client_max_body_size8m;#设定请求缓Sendfile on;#开启高效文件传输模式, the sendfile instruction specifies whether Nginx calls the Sendfile function to output the file, and for normal applications to be set to ON, if it is used for downloading applications such as disk IO heavy load applications, can be off to balance disk and network I/O processing speed, Reduce the load on the system. Note: If the picture does not appear normal, change this to off. AutoIndex on;#开启目录列表访问, the appropriate download server, the default shutdown. Tcp_nopush on;#防止网络阻塞Tcp_nodelay on;#防止网络阻塞Keepalive_timeout -;#长连接超时时间, Unit is seconds#FastCGI相关参数是为了改善网站的性能: Reduce resource usage and improve access speed. The following parameters can be understood by literal means. Fastcgi_connect_timeout -;Fastcgi_send_timeout -;Fastcgi_read_timeout -;fastcgi_buffer_size64k;fastcgi_buffers464k;fastcgi_busy_buffers_size128k;fastcgi_temp_file_write_size128k;#gzip模块设置gzip on;#开启gzip压缩输出Gzip_min_length1k;#最小压缩文件大小gzip_buffers416k;#压缩缓冲区gzip_http_version1.0;#压缩版本 (default 1.1, front End If squid2.5 please use 1.0)Gzip_comp_level2;#压缩等级gzip_types Text/Plain Application/x-JavaScript Text/CSS Application/XML;#压缩类型, the default is already included text/html, so there is no need to write, write up will not have a problem, but there will be a warn. gzip_vary on;#limit_zone crawler $binary _remote_addr 10m; #开启限制IP连接数的时候需要使用Upstream{#upstream的负载均衡, weight is a weight that can be defined based on machine configuration. The Weigth parameter represents weights, and the higher the weight, the greater the probability of being allocated. Server192.168.80.121: theWeight=3;Server192.168.80.122: theWeight=2;Server192.168.80.123: theWeight=3;}

Configuration of the virtual host

Server{#监听端口Listen the;#域名可以有多个, separated by a spaceIndex Index.HTML Index.htm index.PHP;Root/Data/www/ha97; Location~.*. (PHP|php5)?${Fastcgi_pass127.0.0.1:9000;Fastcgi_index Index.PHP;include fastcgi.conf;}#图片缓存时间设置 Location~.*. (gif|jpg|JPEG|PNG|BMP|SWF)${Expires10d;}#JS和CSS缓存时间设置 Location~.*. (JS|CSS)?${Expires1h;}

Log format settings

Log_format Access' $remote _addr-$remote _user [$time _local] "$request" '' $status $body _bytes_sent ' $http _referer '' "$http _user_agent $http _x_forwarded_for ';#定义本虚拟主机的访问日志Access_log/var/Log/Nginx/ha97access.Log Access;#对 "/" Enable reverse proxy Location/{Proxy_pass;Proxy_redirect off;Proxy_set_header X-Real-IP $remote _addr;#后端的Web服务器可以通过X-forwarded-for Get the user real IPProxy_set_header X-forwarded- for$proxy _add_x_forwarded_for;#以下是一些反向代理的配置, Optional. Proxy_set_headerHost$host;client_max_body_size10m;#允许客户端请求的最大单文件字节数client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数,Proxy_connect_timeout -;#nginx跟后端服务器连接超时时间 (proxy connection timed out)Proxy_send_timeout -;#后端服务器数据回传时间 (proxy send timeout)Proxy_read_timeout -;#连接成功后, back-end server response time (proxy receive timeout)proxy_buffer_size4k;#设置代理服务器 (nginx) buffer size for saving user header informationproxy_buffers432k;#proxy_buffers缓冲区, the average Web page is set below 32kproxy_busy_buffers_size64k;#高负荷下缓冲大小 (proxy_buffers*2)proxy_temp_file_write_size64k;#设定缓存文件夹大小, greater than this value, will be passed from the upstream server}

Set the address to view nginx status

 Location/Nginxstatus{Stub_status on;Access_log on;Auth_basic"Nginxstatus";Auth_basic_user_file conf/htpasswd;#htpasswd文件的内容可以用apache提供的htpasswd工具来产生. }#本地动静分离反向代理配置#所有jsp的页面均交由tomcat或resin处理 Location~. (JSP|jspx| Do)?$ {Proxy_set_headerHost$host;Proxy_set_header X-Real-IP $remote _addr;Proxy_set_header X-forwarded- for$proxy _add_x_forwarded_for;Proxy_pass;}#所有静态文件由nginx直接读取不经过tomcat或resin Location~.*. (htm|HTML|gif|jpg|JPEG|PNG|BMP|SWF|IOC|rar|Zip|txt|flv|Mid|Doc|ppt|PDF|xls|mp3|WMA)${Expires15d;} Location~.*. (JS|CSS)?${Expires1h;}}}
Other case sharing

1.thinkphp Path_info Support

You can consider configuring URL rewrite rules to emulate the implementation by referencing thinkphp hidden index.php

2.thinkphp Hidden index.php

thinkphp Config configuration:


Nginx Rewrite configuration:

location /{if(!-e $request_filename){   rewrite  ^(.*)$  /index.php?s=$1  last;break;}}

If your thinkphp is installed in a level two directory, the Nginx pseudo-static method is set as follows, where Youdomain is the directory name

location /youdomain/{if(!-e $request_filename){            rewrite  ^/youdomain/(.*)$  /youdomain/index.php?s=$1  last;}}

Common configuration of Nginx

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: 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.