Detailed description of the Nginx configuration file

Source: Internet
Author: User
Tags php script sendfile

#运行用户

User Www-data;

#启动进程, usually set to equal number of CPUs

Worker_processes 1;

#全局错误日志及PID文件

Error_log/var/log/nginx/error.log;

Pid/var/run/nginx.pid;

#工作模式及连接数上限

Events {

Use Epoll; A way to #epoll是多路复用IO (I/O multiplexing), but only for linux2.6 above the kernel, can greatly improve the performance of Nginx

Worker_connections 1024; #单个后台worker Maximum number of concurrent links for process processes

# multi_accept on;

}

#设定http服务器, using its reverse proxy function to provide load balancing support

HTTP {

#设定mime类型, the type is defined by the Mime.type file

Include/etc/nginx/mime.types;

Default_type Application/octet-stream;

#设定日志格式

Access_log/var/log/nginx/access.log;

#sendfile instruction Specifies whether Nginx calls the Sendfile function (zero copy) to output the file, for general applications,

#必须设为 on, if used for downloading applications such as disk IO heavy load applications, can be set to off to balance disk and network I/O processing speed, reduce system uptime.

Sendfile on;

#tcp_nopush on;

#连接超时时间

#keepalive_timeout 0;

Keepalive_timeout 65;

Tcp_nodelay on;

#开启gzip压缩

gzip on;

Gzip_disable "MSIE [1-6]. (?!. *SV1) ";

#设定请求缓冲

Client_header_buffer_size 1k;

Large_client_header_buffers 4 4k;

include/etc/nginx/conf.d/*.conf;

include/etc/nginx/sites-enabled/*;

#设定负载均衡的服务器列表

Upstream Mysvr {

#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.

#本机上的Squid开启3128端口

Server 192.168.8.1:3128 weight=5;

Server 192.168.8.2:80 weight=1;

Server 192.168.8.3:80 weight=6;

}

server {

#侦听80端口

Listen 80;

#定义使用www. xx.com Access

server_name www.xx.com;

#设定本虚拟主机的访问日志

Access_log Logs/www.xx.com.access.log Main;

#默认请求

Location/{

Root/root; #定义服务器的默认网站根目录位置

Index index.php index.html index.htm; #定义首页索引文件的名称

Fastcgi_pass www.xx.com;

Fastcgi_param script_filename $document _root/$fastcgi _script_name;

Include/etc/nginx/fastcgi_params;

}

# define Error Prompt page

Error_page 502 503 504/50x.html;

Location =/50x.html {

Root/root;

}

#静态文件, Nginx handle it himself.

Location ~ ^/(images|javascript|js|css|flash|media|static)/{

Root/var/www/virtual/htdocs;

#过期30天, the static file is not very updated, the expiration can be set larger, if frequently updated, you can set smaller.

Expires 30d;

}

#PHP script requests are all forwarded to fastcgi processing. Use the fastcgi default configuration.

Location ~. php$ {

Root/root;

Fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

Fastcgi_param Script_filename/home/www/www$fastcgi_script_name;

Include Fastcgi_params;

}

#设定查看Nginx状态的地址

Location/nginxstatus {

Stub_status on;

Access_log on;

Auth_basic "Nginxstatus";

Auth_basic_user_file conf/htpasswd;

}

#禁止访问. htxxx file

Location ~/.ht {

Deny all;

}

}

}

The above are some basic configuration, the biggest advantage of using Nginx is load balancing

If you want to use load balancing, you can modify the configuration HTTP nodes as follows:

#设定http服务器, using its reverse proxy function to provide load balancing support

HTTP {

#设定mime类型, the type is defined by the Mime.type file

Include/etc/nginx/mime.types;

Default_type Application/octet-stream;

#设定日志格式

Access_log/var/log/nginx/access.log;

#省略上文有的一些配置节点

#。。。。。。。。。。

#设定负载均衡的服务器列表

Upstream Mysvr {

#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.

Server 192.168.8.1x:3128 weight=5; #本机上的Squid开启3128端口

Server 192.168.8.2x:80 weight=1;

Server 192.168.8.3x:80 weight=6;

}

Upstream MYSVR2 {

#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.

Server 192.168.8.x:80 weight=1;

Server 192.168.8.x:80 weight=6;

}

#第一个虚拟服务器

server {

#侦听192.168.8.x 80 Ports

Listen 80;

server_name 192.168.8.x;

#对aspx后缀的进行负载均衡请求

Location ~. *.aspx$ {

Root/root; #定义服务器的默认网站根目录位置

Index index.php index.html index.htm; #定义首页索引文件的名称

Proxy_pass http://mysvr #请求转向mysvr defined list of servers

#以下是一些反向代理的配置可删除.

Proxy_redirect off;

#后端的Web服务器可以通过X-forwarded-for get user real IP

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

Client_max_body_size 10m; #允许客户端请求的最大单文件字节数

Client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,

Proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间 (Agent connection timed out)

Proxy_send_timeout 90; #后端服务器数据回传时间 (Agent send timeout)

Proxy_read_timeout 90; #连接成功后, back-end server response time (proxy receive timeout)

Proxy_buffer_size 4k; #设置代理服务器 (Nginx) to save the buffer size for user header information

Proxy_buffers 4 32k; #proxy_buffers缓冲区, the average page below 32k, so set

Proxy_busy_buffers_size 64k; #高负荷下缓冲大小 (proxy_buffers*2)

Proxy_temp_file_write_size 64k; #设定缓存文件夹大小, greater than this value, will be passed from the upstream server

}

}

}

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.