本文链接: https://51meaning.cn/blog/?p=549 转载请注明转载自:www.51meaning.cn
本文针对Nginx配置进行解析与举例,包括一般配置、gzip、静态资源缓存、反向代理、负载均衡、https、TCP/UDP配置等等。
一、Nginx配置结构图
二、Nginx配置详解
#指定Nginx的worker进程运行用户以及用户组,默认由nobody账号运行
#user nobody;
#指定Nginx要开启的进程数,与CPU核数吻合即可
worker_processes 4;
#日志文件的路径和名称,日志输出级别有debug,info,notice,warn,error,crit
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#主进程id号存储的文件位置
#pid logs/nginx.pid;
events {
#工作模式:select(标准工作模式),poll(标准工作模式),kqueue(高效),
#epoll(高效,Linux首选),rtsig,/dev/poll
#use epoll;
#每个进程最大连接数
#nginx最大的连接数max_client=worker_processes * worker_connections。
#进程最大连接数受到系统最大打开文件数限制,需要设置ulimit
worker_connections 1024;
}
http {
include mime.types;
#application/octet-stream支持任意的类型
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"';
#设置日志默认存储目录
#access_log logs/access.log main;
#修改反向代理请求的Header
proxy_pass http://127.0.0.1:8001$request_uri;
proxy_set_header host $http_host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for
#开启高效文件传输模式(零拷贝)
sendfile on;
#tcp_nopush on;
#客户端连接超时时间
keepalive_timeout 65;
#设置是否开启gzip模块(二进制文件不压缩,小文件不压缩,非html文件需指定类型才能压缩)
#gzip on;
#gzip_buffers 4 16K;
#gzip_comp_level 7;
#gzip_min_length 500;
#gzip_types text/css text/xml text/javascript;
#引入其他配置文件
#include vhost/*.conf;
#虚拟主机配置
server {
#虚拟主机的服务端口
listen 80;
#用来指定ip或者域名,多个域名用逗号分开
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://mysvr;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#将以php为后缀的文件转发到FastCGI处理
#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;
#}
#静态文件,nginx自己处理~表示从根目录去找
#location ~ ^/(images|javascript|js|css|flash|media|static)/ {
# root html;
# expires 30d; #缓存时间30天,静态文件更新不多,
# 过期时间可以设大一点。
#}
#alias别名
location ^~ /alias {
alias /usr/local/nginx18/html/images/;
index index.html;
}
#rewrite只能对域名后 非参数部分起作用
location /logo {
rewrite ^/ /alias/timg.gif;
#rewrite ^/logo/(.*)$ /logo.do?p=$1?
}
#nginx状态–with-http_stub_status_module模块
location /statePage {
stub_status;
}
}
#配置nginx负载均衡服务器列表
upstream mysvr {
#负载均衡算法,默认轮询,另外有权重weight, random, hash,
#ip_hash, least_conn;least_time;
#random;
#weigth参数表示权值,权值越高被分配到的几率越大
server 127.0.0.1:88 weight=5;
server 127.0.0.1:80 weight=1;
}
# HTTPS server –with-http_ssl_module
server {
#ssl写在443端口后面。这样http和https的链接都可以用
listen 443 ssl;
#域名绑定
server_name localhost;
#证书(公钥.发送到客户端的)
ssl_certificate server.crt;
# 私钥
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;
}
}
}
#upd/tcp –with-http_stream
stream {
#负载均衡
upstream backend {
server 192.168.3.173:3306;
}
server {
listen 8686;
proxy_connect_timeout 8s;
proxy_timeout 24h; #代理超时
#反向代理
proxy_pass backend;
}
}
三、更多Nginx配置
Linux(CentOS)下Nginx rtmp模块安装配置实现语音直播