"Reprint" "Nginx Operation and Maintenance Basic (2)" Nginx configuration file description and virtual Host Configuration example

Source: Internet
Author: User
Tags epoll sendfile

Transferred from: http://www.cnblogs.com/nixi8/p/4869760.html

Configuration file Description
#定义Nginx运行的用户和用户组User www www;#nginx进程数, it is recommended to set the total CPU core count for the current host.Worker_processes8;#全局错误日志定义类型, [Debug | info | notice | warn | error | crit]Error_log Ar/loginx/error.logInfo#进程文件Pid/usr/local/nginx/logs/nginx.pid;#一个nginx进程打开的最多文件描述符数目, the theoretical value should be the number of open files (the value of the system 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_nofile65535;#工作模式与连接数上限events{#参考事件模型, use [kqueue | rtsig | epoll |/dev/poll | select | poll]; The Epoll model is a high-performance network I/O model in the Linux version 2.6 kernel, and if it runs on FreeBSD, it uses the Kqueue model.UseEpoll;#单个进程最大连接数 (maximum number of connections = number of connections * processes)Worker_connections65535;}#设定http服务器http{Include Mime.types;#文件扩展名与文件类型映射表Default_type Application/octet-stream;#默认文件类型CharSet utf-8;#默认编码Server_names_hash_bucket_size128;#服务器名字的hash表大小Client_header_buffer_size32k;# Customer request Header buffer size Nginx will use client_header_buffer_size this buffer to read the header value by default, if the header is too large, it will use Large_client_header_buffers to readLarge_client_header_buffers464k;Client_max_body_size8m;#设定请求大小;SendfileOn#开启高效文件传输模式, 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.AutoIndexOn#开启目录列表访问, the appropriate download server, the default shutdown.Tcp_nopushOn#防止网络阻塞Tcp_nodelayOn#防止网络阻塞Keepalive_timeout120;#长连接超时时间, Unit is seconds#FastCGI相关参数是为了改善网站的性能: Reduce resource usage and improve access speed. The following parameters can be understood by literal means.Fastcgi_connect_timeout300;Fastcgi_send_timeout300;Fastcgi_read_timeout300;Fastcgi_buffer_size64k;Fastcgi_buffers464k;Fastcgi_busy_buffers_size128k;Fastcgi_temp_file_write_size128k;#gzip模块设置GzipOn#开启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 textml, so there is no need to write, write up will not have a problem, but there will be a warn.Gzip_varyOn#limit_zone crawler $binary _remote_addr 10m; #开启限制IP连接数的时候需要使用Upstream nginx.com {#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:80 weight=3;Server192.168.80.122:80 weight=2;Server192.168.80.123:80 weight=3; }#虚拟主机的配置, a server represents a virtual host, there are a number of repeatable configuration, the recommended use of include, set up multiple hosts can reduce the amount of the configuration file code;server {#监听端口Listen80;#域名可以有多个, separated by a spaceserver_name www.nixi8.com nixi8.com;Index index.html index.htm index.php;Root/data/www/nixi8;##### include enable-php.conf ######资源定位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 Access‘$remote _ADDR-$remote _user [$time _local] "$request "'‘$status$body _bytes_sent "$http _referer "'‘"$http _user_agent "$http _x_forwarded_for ';#定义本虚拟主机的访问日志Access_log/home/wwwlogs/access.log access;#对 "/" Enable reverse ProxyLocation/{Proxy_passhttp://127.0.0.1:88;Proxy_redirectOffProxy_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_header Host$host;Client_max_body_size10m;#允许客户端请求的最大单文件字节数Client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数,Proxy_connect_timeout90;#nginx跟后端服务器连接超时时间 (proxy connection timed out)Proxy_send_timeout90;#后端服务器数据回传时间 (proxy send timeout)Proxy_read_timeout90;#连接成功后, 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_size44);#高负荷下缓冲大小 (proxy_buffers*2)Proxy_temp_file_write_size44);#设定缓存文件夹大小, greater than this value, will be passed from the upstream server}#设定查看Nginx状态的地址Location/nginxstatus {Stub_statusOnAccess_logOnAuth_basic"Nginxstatus";Auth_basic_user_file confpasswd;#htpasswd文件的内容可以用apache提供的htpasswd工具来产生. }#本地动静分离反向代理配置#所有jsp的页面均交由tomcat或resin处理Location~. (JSP|JSPX|DO)? $ {proxy_set_header Host  $host; proxy_set_header x-real-ip  $remote _addr; proxy_set_header x-forwarded-for  $proxy _add_x_forwarded_for ; proxy_pass http://127.0.0.1:8080;}  #所有静态文件由nginx直接读取不经过tomcat或resin location ~. *. (htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma) $ { Expires 15D;} location ~. *. ( JS|CSS)? $ {expires 1H;}}       

After modifying the configuration file, it is a good idea to check the correct configuration file, so as to prevent the server from restarting after the reboot. Determine if the Nginx configuration is the correct command as follows:

/usr/nginx/conf/nginx.conf /usr/nginx/sbin/nginx -t
Virtual Host Configuration
 # domain-based virtual host server {listen 80;  #监听端口 server_name a.com;  #监听域名, multiple domain names separated by a space; location/{root/var/www/a.com;  #站点根目录, where your website files are stored. Note: Site directory and domain name as far as possible, develop a good habit index index.html;} # Port-based virtual host configuration server {listen 8080; server_name 192.168.1.204; location/{root/var/www/b.com; index index.html;}           
Logical operations

The logic in Nginx configuration that does not support the IF condition contains && or | | Operation, and does not support if nested syntax, otherwise the following error will be reported: Nginx: [Emerg] Invalid condition.
We can do this indirectly in the way of variables.

# For example to achieveif ($arg _unitid =42012 &&$uri ~/thumb/) {Return403;} # can be operated as follows set  $flag 0; if ( $uri ~ ^/thumb/[0-9]+_160.jpg$ {set  $flag  " ${flag}1 ";} if ( $arg _unitid = 42012) {set  $flag  " ${flag}1 ";} if ( $flag =  "011") {return 403;}           

"Reprint" "Nginx Operation and Maintenance Basic (2)" Nginx configuration file description and virtual Host Configuration example

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: info-contact@alibabacloud.com 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.