nginx配置文件单独创建和管理

Posted on 2022-09-01 18:33  brad1208  阅读(630)  评论(0编辑  收藏  举报

在nginx主配置文件nginx.conf的http模块下引入配置文件夹(注意路径的正确性)

 

1、nginx主配置文件备份后编辑(nginx配置存放位置:/usr/local/nginx/conf/):

cd /usr/local/nginx/conf/ && mv nginx.conf nginx.conf-bak && vim nginx.conf

贴入内容如下:

user  www www; #运行用户

worker_processes 1;
#nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数

#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个或者将一个进程分配到多个cpu,根据自己环境来配。

error_log /usr/local/nginx/logs/nginx_err.log crit; #错误日志位置和日志级别

pid  /usr/local/nginx/nginx.pid;
#目录和安装位置一致才行,按教程安装的不用改目录

worker_rlimit_nofile 65535;
#当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

events
{
 use epoll;
 #使用epoll的I/O模型,用这个模型来高效处理异步事件
 worker_connections 65535;
 #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。
}

http

  include /usr/local/nginx/conf/conf.d/*.conf;
  #这里就是引入的子配置文件夹

 server_tokens off;
 #隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。

 include    mime.types;
 default_type  application/octet-stream;

 charset  utf-8; #字符集编码格式

 server_names_hash_bucket_size 128;
 client_header_buffer_size 2k;
 #客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

 large_client_header_buffers 4 4k;
 client_max_body_size 8m;

 sendfile on;
 #可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。

 tcp_nopush on;
 #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值

 tcp_nodelay on;
 #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。

 keepalive_timeout 60;
 #http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃。

 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;

 #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。

 open_file_cache max=65535 inactive=20s;
 #这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存
 
 open_file_cache_min_uses 1;
 open_file_cache_valid 30s; #多长时间检查一次缓存的有效信息
 
 gzip on;
 gzip_min_length  1k;
 gzip_buffers   4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types    text/plain application/x-javascript text/css application/xml;
 gzip_vary on;

 log_format   access   '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" $http_x_forwarded_for';
 access_log  /usr/local/nginx/logs/nginx_out.log  access; #访问日志存放位置

autoindex off; #不允许列出整个目录
autoindex_exact_size off; #显示出文件的大概大小,而非确切大小,单位是kB或者MB或者GB
autoindex_localtime off; #显示的文件时间为GMT时间

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;
#限速模块——限制单个IP(或者其他的key)发送请求的速率,超出指定速率后,Nginx将直接拒绝更多的请求;定义了一个名为mylimit大小为10MB的共享内存区域(zone),用来存放限速相关的统计信息,限速的key值为二进制的IP地址($binary_remote_addr),限速上限(rate)为5r/s,每秒5请求;

}


2、在引入的配置文件夹中添加个基础的独立配置文件html.conf,

复制用快捷指令:mkdir /usr/local/nginx/conf/conf.d/ && cd /usr/local/nginx/conf/conf.d/ && vim html.conf

贴入下方绿色文字配置:

server
 {
  listen    80; #监听端口
  server_name localhost; #域名
  index index.html index.htm index.php; #配置增加支持php文件解析
  root  /var/html/; #站点目录,目录要存在

#limit_conn zone=mylimit 5; #限制在记录状态的每个IP只能发起的并发连接数量
#limit_rate 1000k; #对每个连接限速多少,这里是对连接限速,而不是对IP限速。如果一个IP允许三个并发连接,那么这个IP就是限速为limit_rate×3,在设置的时候要根据自己的需要做设置调整,要不然会达不到自己希望的目的。
#limit_rate_after 50000k; #在下载这个数值后开始限速。

  location / { 
    #limit_req zone=mylimit burst=10; #限速模块——每个key(此处是每个IP)最多允许10个突发请求的到来,与下方规则二选一
    limit_req zone=mylimit burst=10 nodelay; #限速模块——nodelay参数允许请求在排队的时候就立即被处理,与上方规则二选一
  }

  location ~ .*\.(php|php5)?$
  {
    fastcgi_pass 127.0.0.1:9000; #遇到php这类动态解析文件丢给9000端口的程序处理
    fastcgi_index index.php; #配置支持php文件解析
    #include test.conf;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
  {
   expires 15d; #浏览器缓存时间1小时
  }

  #安全设置
  location ~* ^/(class|controller|db|data|functions|templates)/.*.(db3|php|php5|sql)$ {
    return 403;
  }
  location ~* ^/(data)/.*.(html)$ {
        deny all;
  }
  location /db {
        deny all;
  }

#伪静态
rewrite ^/click/(.*) /index.php?c=click&id=$1 break;
rewrite ^/api/(.*)?(.*) /index.php?c=api&method=1&2 break;
rewrite /login /index.php?c=login break;

  }

 

3、配置检查

cd /usr/local/nginx/sbin && ./nginx -t

 

4、启动nginx

cd /usr/local/nginx/sbin && ./nginx

 

nginx运行状态查看

查看80端口占用情况:
netstat -tunlp | grep 80

# 查看进程是否运行
ps -A | grep nginx

# 强制关闭nginx
pkill nginx