N g i n x 的配置文件 Nginx的配置文件 Nginx的配置文件
1.Nginx的配置文件的位置
cat /usr/local/nginx/conf/nginx.conf
2. 配置文件内容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.配置文件解析
配置文件中的内容 包含三部分内容
(1)全局块:配置服务器整体运行的配置指令
比如 worker_processes 1;处理并发数的配置
从配置文件开始到events 块之间的内容,主要会设置一些影响ngix服务器整体运行的配置指令
主要包括配置运行Nginx服务器的用户(组)、允许生成的 worker process 数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等。
(2)events 块:影响 Nginx 服务器与用户的网络连接
比如 worker_connections 1024; 支持的最大连接数为 1024
events块涉及的指令主要影响Nginx 服务器与用户的网络连接
常用的设置包括是否开启对多work process下的网络连接进行序列化
是否允许同时接收多个网络连接
选取哪种事件驱动模型来处理连接请求
每个wordprocess可以同时支持的最大连接数等
上述例子就表示每个work process支持的最大连接数为1024
这部分的配置对Nginx的性能影响较大,在实际中应该灵活配置。
(3)http 块 包含两部分:
http 全局块
server 块
Nginx服务器配置中最频繁的部分,代理、缓存和日志定义
等绝大多数功能和第三方模块的配置
都在这里。
反向代理、负载均衡、高可用都在这里配置
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
# 一 全局块:配置服务器整体运行的配置指令
worker_processes 1;
# 二 events 块:影响 Nginx 服务器与用户的网络连接
events {
worker_connections 1024;
}
# 三http 块 还包含两部分: http 全局块 server 块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
反向代理和负载均衡
upstream major{
server 127.0.0.1:5000 weight=1;
server 127.0.0.1:6000 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://major;
client_max_body_size 1024m;
}
实例:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream major{
server 127.0.0.1:5000 weight=1;
server 127.0.0.1:6000 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://major;
client_max_body_size 1024m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
补充:
在nginx中,通过client_max_body_size xxm;
设置nginx服务允许用户最大上传数据
在nginx中根据业务需求上传文件大小限制
设置参数 client_max_body_size xxm;
参数语法 client_max_body_size 具体的大小值,默认1m;
放置位置 http,server,location