nginx 配置文件详解

# 指定nginx运行的用户以及用户组、默认为nobody;
# user nobody;

# 开启的线程数、通常与CPU的数量一致;
# 每个nginx进程消耗的内存是10兆左右;
worker_processes 1;

# 错误日志文件、级别以notice显示;
# 有debug、info、warn、error、crit模式、debug输出最多、crit输出最少;
# error_log logs/error.log;
# error_log logs/error.log notice;
# error_log logs/error.log info;

# 指定进程id的存储文件;
# pid logs/nginx.pid;

# 设置一个nginx进程打开的最多文件描述符数目;
# 理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除;
# 但是nginx分配请求并不均匀、所以建议与ulimit -n的值保持一致;
# 因为nginx调度时分配请求到进程并不是那么的均衡;
# 所以当填写的值小于实际总并发量时、就会返回502错误;
# worker_rlimit_nofile 65535;

# 工作模式及连接数上限;
events {

# 设置工作模式为epoll,
# 有select,poll,kqueue,rtsig和/dev/poll模式;
# epoll是多路复用IO(I/O Multiplexing)中的一种方式;
# 仅用于linux2.6以上内核,可以大大提高nginx的性能;
# use epoll;

# 定义单个进程的最大并发连接数,受系统进程的最大打开文件数量限制;
# 最大连接数=连接数*进程数;
worker_connections 1024;

# 并发总数是 work_processes 和 worker_connections 的乘积;
# max_clients = work_process * worker_connections
# 若设置了反向代理;
# max_clients = work_process * worker_connections / 4
# work_connections 值的设置跟物理内存大小有关;
}

##### Nginx的Http服务器配置,Gzip配置;
http {

# 主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度;
# DNS主配置文件中的zonerfc1912;
# acl基本上都是用include语句;
# 设定mime类型,类型由mime.type文件定义;
include mime.types;

# 核心模块指令、智力默认设置为二进制流;
# 也就是当文件类型未定义时使用这种方式;
default_type application/octet-stream;

# 日志格式的设定,main为日志格式的名称,可自行设置,后面引用;
# 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 logs/access.log main;

# 开启高效文件传输模式;
# sendfile 指令指定nginx 是否调用sendfile 函数(zero、copy方式)来输出文件;
# 对于普通的应用、必须设置为ON;
# 如果用来进行下载应用、IO重负载应用、可设置为OFF;
# 以此来平衡磁盘与IO处理速度、降低系统的uptime
sendfile on;

# 开启防止网络阻塞;
# tcp_nopush on;

# 作用于socket参数TCP_NODELAY;
# 禁用nagle算法,也即不缓存数据;
# nagle缓存算法;
# 有些应用程序在网络通讯的时候会发送很少的字节;
# 比如说一个字节,那么再加TCP协议本身;
# 实际上发的要41个字节,这样的效率是很低的;
# 这时候nagle算法就应运而生了;
# 它将要发送的数据存放在缓存里;
# 当积累到一定量或一定时间,再将它们发送出去;
# tcp_nodelay on;

# 设置客户端连接保存活动的超时时间;
# keepalive_timeout 0;
keepalive_timeout 65;
# 客户请求头缓冲大小;
# nginx默认会用client_header_buffer_size这个buffer来读取header值;
# 如果header过大,它会使用large_client_header_buffers来读取;
# 如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request;
# 如果超过buffer,就会报HTTP 414错误(URI Too Long);
# nginx接受最长的HTTP头部大小必须比其中一个buffer大;
# 否则就会报400的HTTP错误(Bad Request);
# 设定上传文件大小限制;
client_header_buffer_size 256k;

# 设定请求缓存;
large_client_header_buffers 4 256k;

# 服务器名字的hash表大小;
server_names_hash_bucket_size 128;

#
client_max_body_size 100m;

# FastCGI相关参数是为了改善网站的性能;
# 减少资源占用,提高访问速度;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

##### HttpGZip模块配置;
# httpGzip modules
# 开启gzip压缩;
# gzip on;
gzip on;
# 设置允许压缩的页面最小字节数;
gzip_min_length 1k;
# 申请4个单位为32K的内存作为压缩结果流缓存;
gzip_buffers 4 32k;
# 设置识别http协议的版本,默认为1.1;
gzip_http_version 1.1;
# 指定gzip压缩比,1-9数字越小,压缩比越小,速度越快;
gzip_comp_level 2;
# 指定压缩的类型;
gzip_types text/plain application/x-javascript text/css application/xml;
#让前端的缓存服务器进过gzip压缩的页面;
gzip_vary on;
gzip_disable "MSIE [1-6].";

# 设定虚拟主机配置;
server {

# 监听端口为:80;
listen 80;

# 设置主机域名;
server_name localhost;

# 定义服务器的默认网站根目录位置;
root "D:/phpStudy/WWW";

# 设置访问的与语言编码;
# charset koi8-r;

# 设置虚拟主机访问日志的存放路径及日志的格式为main;
# access_log logs/host.access.log main;

# nginx跨域;

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header PS 1;

# 设置虚拟主机的基本信息;
location / {

# 设置虚拟主机默认访问的网页;
index index.html index.htm index.php l.php;

# 开启访问是目录显示;
autoindex on;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}

# PHP脚本的请求全部转发到FastCGI处理;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
# 禁止访问 .htxxxx的文件;
# location ~ /.ht {
# deny all;
# }

}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;

# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

include vhosts.conf;

}

#################################

暂时到这、以后用到、及时改正;

如有错误、请告知;

原文地址:https://www.cnblogs.com/laowenBlog/p/6774455.html