美文网首页
Nginx安装配置

Nginx安装配置

作者: 茧铭 | 来源:发表于2020-11-16 17:41 被阅读0次

安装nginx

1.创建文件夹 mkdir /var/temp/nginx -p 不创建要报错

2.配置

./configure \
--prefix=/usr/local/nginx \                         nginx安装目录
--pid-path=/var/run/nginx/nginx.pid \               
--lock-path=/var/lock/nginx/lock \                  锁定安装文件,防止被恶意篡改和误操作
--error-log-path=/var/log/nginx/error.log \         错误日志
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \                    启动gzip模块,在线实时压缩输出数据
--http-client-body-temp-path=/var/temp/nginx/client \客户端请求的临时目录
--http-proxy-temp-path=/var/temp/nginx/proxy \      设定http代理临时目录
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

3.编译和安装 make && make install

进程模型

一个master 多个worker进程,nginx.conf最顶部的配置决定worker的数量

worker与客户端交互,master接收来自管理者的命令 如 quit reload 等

抢占机制

1.client访问进来之后有一个accept_mutex,相当于互斥锁,worker竞争这把锁,抢到了才能去处理这个请求

2.面对阻塞请求

传统服务器

如果某个worker被某个请求阻塞了之后,新的请求不能接入这个worker,同步阻塞了。
master会重新fork一个worker来处理新的请求。
循环往复之后会消耗更多机器性能,因为开启的进程过多

nginx

  worker阻塞了之后会处理其他请求(异步非阻塞),linux使用了epoll事件机制,epoll机制使nginx进程能处理6w-8w请求,因此多个worker进程下的nginx性能很强,增强CPU性能即可。连接数过高会是CPU超负荷

events {
    use epoll;                      #默认使用epoll
    worker_connnections 1024;        #每个worker允许连接的最大连接数
}

总结:nginx连接数高的原因: 1.抢占机制; 2.epoll模型,异步非阻塞

配置文件

main 全局配置

event               配置工作模式以及连接数

http                    http模块相关配置

    server      

        location    路由规则

        upstrem     集群,内网服务器



#user  nobody;          master主进程默认root用户,worker进程默认属于nobody用户,可以修改为root用户
worker_processes  1;     配置worker进程数量,可以设置为何CPU个数一样,如有别的服务,减去其他进程的数量

#debug  info notice  warn  error  crit     日志级别,前面以及配置了,这里可以默认
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;                 进程号,也配置过了,默认


events {
    # use epoll;                        默认使用epoll(linux), max和windows的模式要改
    worker_connections  1024;            每个worker允许连接的最大连接数,根据硬件配置设置
}


http {
    include       mime.types;       include是导入指令,mime.types在conf中,文件类型
    default_type  application/octet-stream;
    gzip on;                    压缩请求响应,体积变小,会让请求变快(压缩消耗CPU性能)
    gzip_static on;
    gzip_buffers 4 16k;
    gzip_comp_level 5;
    gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
   
    日志格式配置,日志上面配置在 /var/log/nginx/1
    #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;         sendfile开启了才有效,数据包达到一定大小再提交,提升性能

    #keepalive_timeout  0;      不使用客户端连接超时时间
    keepalive_timeout  65;      客户端ctp连接超时时间,单位是秒。一个客户端http完成后,暂时不会关闭连接,在没有超时的情况下继续使用这个连接,节省了资源的开销。不使用每次都要创建连接。
   
    server {
        listen       443;
        server_name  www.nagasiren.cn;
    
        ssl on;
        ssl_certificate /usr/local/nginx/conf/ssl/onebittech.crt;
        ssl_certificate_key /usr/local/nginx/conf/ssl/onebittech.key;
        ssl_session_timeout 5m;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        # 后端服务  
        location /web/ {
            client_max_body_size 100M;
            proxy_set_header Host $host;
            proxy_pass   http://192.168.0.170:8080/web/;
        }
        # 静态资源
        location / {
            root  html;
            index  index.html index.htm;
        }
        # Vue打包项目
        location /online/ {
            root  html;
            index  index.html index.htm;
            try_files $uri $uri/ @router;
        }
        # vue-router
        location @router {
            rewrite ^.*$ /online/index.html last;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
         listen 80;
         server_name www.nagasiren.cn;
         if ($http_name ~* "^online$" ) {
            rewrite ^/(.*)$ https://www.nagasiren.cn/online/ permanent;
         }
         rewrite ^ https://$host$1 permanent;
    }
}

多个服务可以使用include导入

例如 test.conf如下,上面内容一样的就可以使用include test.conf替代

server {
     listen 80;
     server_name www.nagasiren.cn;
     if ($http_name ~* "^online$" ) {
        rewrite ^/(.*)$ https://www.nagasiren.cn/online/ permanent;
     }
     rewrite ^ https://$host$1 permanent;
}

常用命令

./nginx -s stop  暴力关闭
./nginx -s quit  不接受新的请求,等已存在的连接完成后再关闭
./nginx -t      配置文件更改后,检查配置文件对不对(语法、配置等)
./nginx -v      查看版本
./nginx -V      查看更完整的信息,版本和安装配置等
./nginx -? 或 ./nginx -h     查看帮助
./nginx -c /usr/local/nginx/conf/nginx.conf   指定配置文件(pid内容错误可以解决)

日志切割

手动方法
 #!/bin/bash
 LOG_PATH="/var/log/nginx/"
 RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d)
 PID=/var/run/nginx/nginx.pid
 mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
 mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
 #向Nginx主进程发送信号,用于重新打开日志文件
 kill -USR1 `cat $PID`
自动定时

使用cron服务

配置文件

1、路由配置

server {
    listen  80;
    server_name   localhost;
    location / {
        root    /home/online;
        index   index.html;
    }
    # 【2】
    location  /naga {
        root  /home;
    }
    # 【3】
    location  /static {
        alias  /home/naga;
    }
}

第二个例子中,naga文件夹必须在home文件夹下才能访问

第三个例子和第二个路径一样,只不过是用了别名的形式替代

2、开启Gzip压缩功能

gzip  on;              开启功能,提高传输效率,节约带宽
gzip_min_length  1;     限制最小压缩,小于1字节的文件不压缩
gzip_comp_level  3;     压缩比,文件越大,压缩越多,但是CPU使用会越多
gzip_types  text/plain  application/javascript  application/x-javascript  text/css  application/xml  text/javascript  application/x-httpd-php  image/jpeg  image/gif  image/png  application/json;      定义压缩文件的类型

3、匹配规则

1)默认匹配规则

location / { ... } 如上面的例子

2)精确匹配

location = / { ... } 只能匹配到index配置的文件,下面还有别的文件的话不能匹配到

location = /naga/img/face1.png { root: /home } 就只能匹配到 /home/naga/img/face1.png

3)正则表达式匹配

~是抬头是一个正则表达,*表示不区分大小写(没有则区分大小写)

location ~* \.(gif|png|jpe|jpeg) {
  root /home
}

^ 是非的意思,^~表示不实用正则表达式,只能获取home/naga/img下面的内容

location ^~ /naga/img  {
  root /home
}

相关文章

网友评论

      本文标题:Nginx安装配置

      本文链接:https://www.haomeiwen.com/subject/oqezbktx.html