LNMP部署以及Nginx配置的详细解读

nginx的特性

nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:

  • 在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
  • 使用epoll and kqueue作为开发模型
  • nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
  • nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多

nginx的优点

  • 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
  • 内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M)
  • 配置文件非常简单:风格跟程序一样通俗易懂
  • 成本低廉:nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币
  • 支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
  • 内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
  • 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
  • 稳定性高:用于反向代理,宕机的概率微乎其微
  • 模块化设计:模块可以动态编译
  • 外围支持好:文档全,二次开发和模块较多
  • 支持热部署:可以不停机重载配置文件
  • 支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化

nginx的基本功能

  • 静态资源的web服务器,能缓存打开的文件描述符
  • http、smtp、pop3协议的反向代理服务器
  • 缓存加速、负载均衡
  • 支持FastCGI(fpm,LNMP),uWSGI(Python)等
  • 模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
  • 支持SSL

 nginx的扩展功能

  • 基于名称和IP的虚拟主机
  • 支持keepalive
  • 支持平滑升级
  • 定制访问日志、支持使用日志缓冲区提高日志存储性能
  • 支持URL重写
  • 支持路径别名
  • 支持基于IP及用户的访问控制
  • 支持速率限制,支持并发数限制

nginx的应用类别

  • 使用nginx结合FastCGI运行PHP、JSP、Perl等程序
  • 使用nginx作反向代理、负载均衡、规则过滤
  • 使用nginx运行静态HTML网页、图片
  • nginx与其他新技术的结合应用

nginx的工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。

启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。

在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request

部署Nginx

[root@localhost src]# ls
debug  kernels  nginx-1.22.0.tar.gz
//提前下载好Nginx的包 可以在nginx官方网站下载

[root@localhost src]#  useradd -r -M -s /sbin/nologin nginx
[root@localhost src]# mkdir /var/log/nginx
[root@localhost src]# chown -R nginx.nginx /var/log/nginx/
[root@localhost src]# ll -d /var/log/nginx/
drwxr-xr-x. 2 nginx nginx 6 Sep  3 14:32 /var/log/nginx/
//创建nginx用户 和它的日志存放目录。修改权限

[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
//安装依赖包和需要用到的工具

[root@localhost src]# tar xf nginx-1.22.0.tar.gz
[root@localhost src]# ls
debug  kernels  nginx-1.22.0  nginx-1.22.0.tar.gz
[root@localhost src]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
meke && make install
//解压之后编译安装 
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]#  source /etc/profile.d/nginx.sh 
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           128                       [::]:22                     [::]:*   

//设置好环境变量后刷新一下 开启

关闭防火墙和selinux我们访问一下

[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service nginx.service
[root@localhost system]# cat nginx.service 
Documentation=man:sshd(8) man:sshd_config(5)
Wants=sshd-keygen.tar`get
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/local/nginx/sbin/nginx
Restart=on-failure
RestartSec=42s
KillMode=process
[Unit]
Description=nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload

[Install]
WantedBy=multi-user.target

//设置开机自启


[root@localhost ~]# nginx -s stop
[root@localhost ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]#  systemctl status nginx
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-09-03 14:52:40 CST; 8s ago
  Process: 24516 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 24517 (nginx)
    Tasks: 2 (limit: 23458)
   Memory: 2.3M
   CGroup: /system.slice/nginx.service
           ├─24517 nginx: master process /usr/local/nginx/sbin/nginx
           └─24518 nginx: worker process

Sep 03 14:52:40 localhost.localdomain systemd[1]: Starting nginx server daemon...
Sep 03 14:52:40 localhost.localdomain systemd[1]: Started nginx server daemon.

接下来二进制安装Mysql

[root@localhost src]# ls
debug  kernels  mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz  nginx-1.22.0  nginx-1.22.0.tar.gz
//提前把安装包下载好

[root@localhost src]# useradd -r -M -s /sbin/nologin mysql
[root@localhost src]# yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户安装依赖包

[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64 mysql

[root@localhost local]# chown -R mysql.mysql mysql
//解压后更改属主跟名称

[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# which mysql
/usr/local/mysql/bin/mysql
//设置环境变量

[root@localhost local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost local]# vi /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
//做软连接 配置lib库

[root@localhost local]# ldconfig
[root@localhost local]# vi /etc/man_db.conf

MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man
//修改man文档


[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]#  ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Sep  3 15:03 data
//创建data目录,存放舒适化密码

//初始化数据库 添加-insecure 初始化是不生成密码可以免密登录
[root@localhost ~]#  mysqld --initialize-insecure --user mysql --datadir /opt/data


[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//修改配置文件

[root@localhost mysql]# cd support-files/
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]#  chown -R mysql.mysql mysqld
[root@localhost support-files]# ll
total 36
-rw-r--r--. 1 mysql mysql   773 Nov 30  2021 magic
-rwxr-xr-x. 1 mysql mysql 10576 Sep  3 15:07 mysqld
-rwxr-xr-x. 1 mysql mysql  1061 Nov 30  2021 mysqld_multi.server
-rwxr-xr-x. 1 mysql mysql   894 Nov 30  2021 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 Nov 30  2021 mysql.server
[root@localhost support-files]# vi mysqld

basedir=/usr/local/mysql
datadir=/opt/data
[root@localhost support-files]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
 SUCCESS!
//配置脚本启动
[root@localhost ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           80                           *:3306                      *:*                      
LISTEN      0           128                       [::]:22                     [::]:*  


[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
//修改密码
[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
//试试新密码是否能登陆


[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# vi mysqld.service

[Unit]
Description=mysql server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

//设置开机自启


[root@localhost system]# systemctl daemon-reload
[root@localhost system]# pkill mysql
[root@localhost system]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           128                       [::]:22                     [::]:*  


[root@localhost system]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost system]# systemctl status mysqld
● mysqld.service - mysql server daemon
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-09-03 15:12:15 CST; 5s ago
  Process: 27585 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUCCESS)
 Main PID: 27598 (mysqld_safe)
    Tasks: 28 (limit: 23458)
   Memory: 179.8M
//设置开启自启立即启动

PHP安装部署

[root@localhost src]# ls
debug    mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz  nginx-1.22.0.tar.gz
kernels  nginx-1.22.0                                php-7.4.29.tar.xz
//还是一样 提前下好包
[root@localhost src]# tar xf php-7.4.29.tar.xz 
[root@localhost src]# ls
debug    mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz  nginx-1.22.0.tar.gz  php-7.4.29.tar.xz
kernels  nginx-1.22.0                                php-7.4.29
//解压

[root@localhost src]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make ncurses-compat-libs openssl cmake mariadb-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel sqlite-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

//安装依赖包

[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php7 \
> --with-config-file-path=/etc \
>    --enable-fpm \
>    --enable-inline-optimization \
>    --disable-debug \
>    --disable-rpath \
>    --enable-shared \
>    --enable-soap \
>    --with-openssl \
>    --enable-bcmath \
>    --with-iconv \
>    --with-bz2 \
>    --enable-calendar \
>    --with-curl \
>    --enable-exif  \
>    --enable-ftp \
>    --enable-gd \
>    --with-jpeg \
>    --with-zlib-dir \
>    --with-freetype \
>    --with-gettext \
>    --enable-json \
>    --enable-mbstring \
>    --enable-pdo \
>    --with-mysqli=mysqlnd \
>    --with-pdo-mysql=mysqlnd \
>    --with-readline \
>    --enable-shmop \
>    --enable-simplexml \
>    --enable-sockets \
>    --with-zip \
>    --enable-mysqlnd-compression-support \
>    --with-pear \
>    --enable-pcntl \
>    --enable-posix
//编译安装
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.
make make install 
[root@localhost php-7.4.29]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost php-7.4.29]# source /etc/profile.d/php7.sh 
[root@localhost php-7.4.29]# which php
/usr/local/php7/bin/php
//设置环境变量
[root@localhost php-7.4.29]# cp php.ini-production /etc/php.ini   /将生产环境文件 复制到etc下
[root@localhost php-7.4.29]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.29]# chmod +x /etc/rc.d/init.d/php-fpm    //此文件需要执行权限所以复制过去要看是否有执行(x)权限
[root@localhost php-7.4.29]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf     //将php-fpm.conf.default 复制一份名为php-fpm.conf
[root@localhost php-7.4.29]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
 //将www.conf.default 复制一份名为www.conf


[root@localhost php-7.4.29]# service php-fpm start
Starting php-fpm  done
[root@localhost php-7.4.29]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           80                           *:3306                      *:*                      
LISTEN      0           128                       [::]:22                     [::]:*        
//启动查看9000端口

接下来配置nginx

[root@localhost ~]# vi /usr/local/nginx/html/index.php

<?php
    phpinfo();
?>

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vi nginx.conf

location / {
            root   
            index  index.php index.html index.htm;   //在index后面添加index.php,表示优先访问php页面
        }


location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
//这几行类容取消注释 修改

[root@localhost conf]#  nginx -s stop
[root@localhost conf]# nginx 
[root@localhost conf]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                  127.0.0.1:9000                0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*                      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           80                           *:3306                      *:*                      
LISTEN      0           128                       [::]:22                     [::]:* 
//重启一下让配置文件生效

接下来访问php界面

 Nginx的相关配置详细解读

nginx的配置文件详解

主配置文件:/usr/local/nginx/conf/nginx.conf

  • 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
  • 可以在启动nginx时通过-c选项来指定要读取的配置文件

nginx常见的配置文件及其作用

配置文件作用
nginx.confnginx的基本配置文件
mime.typesMIME类型关联的扩展文件
fastcgi.conf与fastcgi相关的配置
proxy.conf与proxy相关的配置
sites.conf配置nginx提供的网站,包括虚拟主机

 nginx.conf配置文件详解

nginx.conf的内容分为以下几段:

main配置段:配置影响nginx全局的指令
配置运行Nginx服务器用户(组)
worker process数
Nginx进程
PID存放路径错误日志的存放路径
配置文件的引入
events配置段:配置影响nginx服务器或与用户的网络连接
设置网络连接的序列化
是否允许同时接收多个网络连接
事件驱动模型的选择
最大连接数的配置
http配置段:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
定义MIMI-Type
自定义服务日志
允许sendfile方式传输文件
连接超时时间
单连接请求数上限
server段:配置虚拟主机的相关参数,一个http中可以有多个server
配置网络监听
基于名称的虚拟主机配置
基于IP的虚拟主机配置
location段:配置请求的路由,以及各种页面的处理情况
location配置
请求根目录配置更改
location的URI
网站默认首页配置
每个配置指令要以分号结尾

语法:derective value1 [value2 ...];

配置文件支持使用变量


内置变量:模块会提供内建变量定义
自定义变量:set var_name value

配置运行Nginx服务器用户(组)


指令格式:user user [group];
    user:指定可以运行Nginx服务器的用户
    group:可选项,可以运行Nginx服务器的用户组

如果user指令不配置或者配置为user nobody nobody,则默认所有用户都可以启动Nginx进程

是否以守护进程方式运行nginx


指令格式:daemon {on|off};

是否以守护进程方式运行nginx,调试时应设置为off

worker process数配置


Nginx服务器实现并发处理服务的关键

指令格式:worker_processes number | auto;
    number:Nginx进程最多可以产生的worker process数
    auto:Nginx进程将自动检测

为了避免上下文切换,通常number值设置为cpu总核心数-1或等于总核心数

将进程绑定到某cpu中,避免频繁刷新缓存


指令格式:worker_cpu_affinity cpumask ...;
    cpumask:使用8位二进制表示cpu核心

例:
worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;
一共四核,0001表示第一个核心,0010表示第二个核心,0100表示第三个核心,1000表示第四个核心

Nginx进程PID存放路径


指令格式:pid file;
    file:指定存放路径和文件名称如果不指定默认置于路径 logs/nginx.pid

Nginx进程是作为系统守护进程在运行,需要在某文件中保存当前运行程序的主进程号,Nginx支持该保存文件路径的自定义

错误日志的存放路径


指令格式:error_log 位置 级别;
    位置:file | stderr | syslog:server=address[,parameter=value] | memory:size
    级别:debug | info | notice | warn | error | crit | alert | emerg
        若要使用debug级别,需要在编译nginx时使用--with-debug选项

设置所有worker进程最大可以打开的文件数


指令格式:worker_rlimit_nofile number;

设置所有worker进程最大可以打开的文件数,默认为1024

配置文件的引入


指令格式:include file;

该指令主要用于将其他的Nginx配置或者第三方模块的配置引用到当前的主配置文件中

计时器解析度配置


指令格式:timer_resolution interval;

降低工作过程中计时器解析度,从而减少系统调用次数

例:
timer_resolution 100ms;

worker进程的nice值配置


指令格式:worker_priority number;

定义工作进程的优先级,允许的范围通常从 -20 到 20 变化

网络连接相关的配置参数

keepalive_timeout number;    //长连接的超时时长,默认为65s
keepalive_requests number;    //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none];    //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off;    //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number;    //读取http请求报文首部的超时时长
client_body_timeout number;    //读取http请求报文body部分的超时时长
send_timeout number;    //发送响应报文的超时时长

是否允许同时接收多个网络连接

指令格式:multi_accept on | off;

该指令默认为off状态,意指每个worker process 一次只能接收一个新到达的网络连接。若想让每个Nginx的
workerprocess都有能力同时接收多个网络连接,则需要开启此配置

nginx作为web服务器时使用的配置:http{}段的配置参数

定义MIME-Type

指令格式:include mime.types;
	    default_type mime-type;

MIME-Type指的是网络资源的媒体类型,也即前端请求的资源类型
include指令将mime.types文件包含进来

cat mime.types
查看mime.types文件内容,发现其就是一个types结构,里面包含了各种浏览器能够识别的MIME类型以及对应类型的文件后缀名字

 自定义服务日志

指令格式:access_log path [format];

	path:自定义服务日志的路径
	format:可选项,自定义服务日志的字符串格式。其也可以使用 log_format 定义的格式

允许sendfile方式传输文件

指令格式:sendfile on | off;
	    sendfile_max_chunk size;

前者用于开启或关闭使用sendfile()传输文件,默认off
后者指令若size>0,则Nginx进程的每个workerprocess每次调用sendfile()传输的数据了最大不能超出此值;若size=0则表示不限制。默认值为0

长连接超时时间配置

指令格式:keepalive_timeout timeout [header_timeout];

timeout 表示server端对连接的保持时间,默认75秒
header_timeout 为可选项,表示在应答报文头部的 Keep-Alive 域设置超时时间:“Keep-Alive :timeout = header_timeout”

长连接请求数上限

指令格式:keepalive_requests number;

该指令用于限制用户在一个长连接上所能够允许请求的最大资源数

为指定类型的UserAgent禁用长连接

指令格式:keepalive_disable [msie6|safari|none...];

默认为keepalive_disable msie6;

是否对长连接使用TCP_NODELAY选项

指令格式:tcp_nodelay on|off;

为了提升用户体验,通常设为on,默认为on

读取http请求报文首部的超时时长

指令格式:client_header_timeout number;

定义读取客户端请求标头的超时。如果客户端未在此时间内传输整个标头,则请求将终止 408(请求退出)错误,默认为60s

配置网络监听

指令格式:

第一种:配置监听的IP地址:
listen IP[:PORT];

第二种:配置监听的端口:
listen PORT;

例:
listen 192.168.207.129:8080;  # 监听具体IP和具体端口上的连接
listen 192.168.207.129;       # 监听IP上所有端口上的连接
listen 8080;                  # 监听具体端口上的所有IP的连接

基于名称和IP的虚拟主机配置

指令格式:server_name name1 name2 ...

name可以有多个并列名称,而且此处的name支持正则表达式书写

例:
server_name ~^www\d+\.myserver\.com$
此时表示该虚拟主机可以接收类似域名  www1.myserver.com  等的请求,而拒绝  www.myserver.com  的域名请求,
所以说用正则表达式可以实现更精准的控制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值