nginx,不管是运维,还是后台程序,都离不开nginx。它的功能真的是很强大。https://www.cnblogs.com/liang-wei/p/5849771.html,这是前辈留下的精品,有时间可以去看看。我只取了一些自己需要的。怕忘记了就当一个备忘录的写吧。
大家学习之前可以去看看nginx中文文档
worker_processes 4;
error_log /work/nginx/log/error.log info;
pid /var/run/nginx.pid;
events{
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024; #;单个后台worker process进程的最大并发链接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections 反向代理模式下: worker_processes * worker_connections/4
}
http{
include conf/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型
#limit模块,可防范一定量的DDOS攻击
#用来存储session会话的状态,如下是为session分配一个名为one的10M的内存存储区,限制了每秒只接受一个ip的一次请求 1r/s
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量
tcp_nopush on;
#激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
tcp_nodelay on;
#FastCGI相关参数:为了改善网站性能:减少资源占用,提高访问速度)
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#设定请求缓存
server_names_hash_bucket_size 128;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
client_max_body_size 100m;
#隐藏响应header和错误通知中的版本号
server_tokens off;
#开启高效传输模式
sendfile on;
#连接超时时间,单位是秒
keepalive_timeout 60;
#开启gzip压缩功能
gzip on;
gzip_min_length 1100;
gzip_buffers 4 16k;
gzip_types text/plain;#一般text/plain类型都需要压缩
#设置日志模式
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#upstream表示负载服务器池
upstream dispatcher {
server 127.0.0.1:9233 weight=1 max_fails=2 fail_timeout=30s;
#server 127.0.0.1:9244 weight=1 max_fails=2 fail_timeout=30s;
#server 127.0.0.1:9255 weight=1 max_fails=2 fail_timeout=30s;
#server 127.0.0.1:9266 weight=1 max_fails=2 fail_timeout=30s;
#可以配置多台
#设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。
#设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。
#这里是在30s内尝试2次失败即认为主机不可用!
}
server{
listen 80;
server_name 192.168.31.185;
access_log /var/log/nginx.access_log main
#error_page 500 502 404 /templates/kumi/phpcms/404.html;当报500 502 404的错误时,可以直接映射到自己的错误页面。
location / {
try_files $uri $uri/ =404;
}
location /imags/ {
root /home/upload/;
autoindex on;
}
location /logo/ {
root /home/upload/coin_picture/;
autoindex on;
}
location /flash/ {
root /home/upload/;
autoindex on;
}
location /share/ {
root /home/upload/;
autoindex on;
}
location ^~ /.well-known/ {
#alias /work/solian-web;
default_type "text/plain";
allow all;
}
location ~ ^/list {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 301 302 304 1d;
#proxy_cache_valid any 1d;
proxy_pass http://dispatcher;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
if ($request_method = 'OPTIONS') {
return 204;
}
expires 1d;
}
}
}
nginx 的一个坑。当nginx.conf配置有错误的时候。当你重启完毕。其实你新改的配置是不生效的。(坑)