nginx基础(一)

参考视频:https://www.bilibili.com/video/BV1H44y1b775?p=5&spm_id_from=pageDriver
[root@Web html]# cat /etc/nginx/nginx.conf | grep -v -E '^.*#|^$'

user nginx;                                              #用户所有者 当html的权限是600 则会有403没有权限报错 当html的所有者为nginx就会显示出网页
worker_processes auto;                          #根据有多少核数,就有几个(worker)子进程 2核就两个worker进程 auto是指cpu
error_log /var/log/nginx/error.log;            #日志目录
pid /run/nginx.pid;                                    #主配置文件进程号,stop reload 没有它执行不了
include /usr/share/nginx/modules/*.conf;
events {                                                                 #定义事件驱动的相关配置,该配置与连接的处理密切相关,最重要的指令如下:
             use method;                                             #定义nginx使用哪种事件驱动类型,在Redhat/CentOS 中性能最好的是epoll模型
             worker_connections number;                   #定义每个worker进程可以处理的连接数
             accept_mutex on|off;                                #处理新连接的方法,on是指由各个worker进程轮流处理,off则会通知所有worker进程,但是只有一个worker进程获得处理连接的权限
                                                                                注意:当启动了accept_mutex on,则会启用accept_mutex_delay 500ms;参数时间可以设置,表示当一个worker进程在处理新连接的时,多长时间以后才会重新接收下一个新的请求
              worker_connections 1024; #表示worker的最大连接数
}


http {
         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 /var/log/nginx/access.log main;
         sendfile                        on;
         tcp_nopush                  on;
         tcp_nodelay                  on;
         keepalive_timeout         65;
         types_hash_max_size   4096;
         include                           /etc/nginx/mime.types;
         default_type                   application/octet-stream;
         include                          /etc/nginx/conf.d/*.conf;
         server {
                      listen          80;
                      listen          [::]:80;
                      server_name    _;
                      root            /data/html;
                      include       /etc/nginx/default.d/*.conf;
                      error_page   404    /404.html;
                      location  =  /404.html {
                      }
                      error_page 500 502 503 504 /50x.html;
                      location = /50x.html {
                      }
          }
}

安装方式:tar包安装
patch -p1 < /root/ngx_http_proxy_connect_module-master/patch/proxy_connect.patch
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \ --with-mail_ssl_module \
--with-file-aio \ --with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--add-module=/root/ngx_http_proxy_connect_module-master

./configure \

--prefix=/data/prog/nginx-1.18.0 \
--user=www \
--group=www \
--pid-path=/data/apd/nginx/nginx.pid \
--error-log-path=/data/logs/nginx/error.log \
--http-log-path=/data/logs/nginx/access.log \
--http-client-body-temp-path=/data/apd/nginx/client_body_temp \
--http-proxy-temp-path=/data/apd/nginx/proxy_temp \
--http-fastcgi-temp-path=/data/apd/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/data/apd/nginx/uwsgi_temp \
--http-scgi-temp-path=/data/apd/nginx/scgi_temp \
--with-pcre \
--with-stream \
--with-http_ssl_module \
--with-http_stub_status_module


yum安装
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

yum -y install nginx
iptables -F
iptanles -I INPUT -p tcp --dport 80 -j ACCEPT

抓包工具
Wireshark

内核调优参数,三个队列
net.core.netdev_max_backlog: 接收自网卡,但未被内核协议栈处理的报文队列长度
[root@Web nginx]# sysctl -a | grep 'net.core.netdev_max_backlog'
net.core.netdev_max_backlog = 1000
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"


net.ipv4.tcp_max_syn_backlog: syn_RCVD状态(半连接)状态
[root@Web nginx]# sysctl -a | grep 'net.ipv4.tcp_max_syn_backlog'

sysctl: reading key "net.ipv6.conf.all.stable_secret"
net.ipv4.tcp_max_syn_backlog = 128
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens33.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"


backlog:全连接队列也就是accept队列
[root@Web nginx]# ss -tnl
State               Recv-Q Send-Q                          Local Address:Port                         Peer Address:Port
LISTEN           0                                                 128                                                  *:22 *:*
LISTEN           0                                                 100                                                  127.0.0.1:25 *:*
LISTEN           0                                                 128                                                   *:80 *:*
LISTEN           0                                                 128                                                   [::]:22 [::]:*
LISTEN           0                                                 100                                                   [::1]:25 [::]:*
LISTEN           0                                                 128                                                   [::]:80 [::]:*


Nginx
一:简介
1.nginx是一个支持高性能、高并发的Web服务软件,它具有很多优越的特性,作为Web服务器,和Apache相比,nginx能够支持更多的并发连接访问,而占用的资源更少,效率更高
1)nginx是一个静态web服务软件,使用nginx运行HTML JS CSS 小图片等静态数据
2)支持动静分离
apache lighttpd IIS node JS
3)支持动态WEB服务扩展
PHP(fastcgi_pass)
JAVA(proxy_pass)
Python(uwsgi_pass)
=====================
memcache(memcache_pass)
...........
Nginx结合FastCGI运行PHP动态程序(使用fastcgi_pass方式)
Nginx结合proxy_pass支持tomcat动态程序(使用proxy_pass)
Nginx结合uwsgi_pass支持Python(使用uwsgi_pass)
4)支持安全的Web服务(https)

2.正向代理:局域网想上网就通过一台代理服务器上网,节省带宽资源,就是由内向外,代替局域网内的PC请求外部应用服务
   反向代理:客户请求访问或者资源,代理服务器去调取服务器的资源,就是由外向内,代替web的用户请求内部的应用服务
   负载均衡:将请求后抛给后面服务器,请求转发的功能(LVS)
1)负载均衡的同类软件
    haproxy,lvs,硬件:F5 , netscaler
    nginx早期只支持http,现在也支持tcp/udp
    支持tcp负载:负载mysql、应用服务
    Nginx haproxy LVS区别与优点
    https://blog.csdn.net/qlj324513/article/details/81541282

3.缓存服务器
      在web缓存服务方面,nginx可通过自身的proxy_cache模块实现类squid等专业缓存软件的功能
1)常见缓存软件
      squid varnish nginx ats

4.nginx核心特点:静态小文件高并发,占用资源少,软件本身小
企业卖你是时需要解答如下nginx http服务器的特色及优点
1)支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
2)资源消耗少:在3万并发连接下,开启10个nginx线程消耗不到200M内存
3)可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的haproxy软件或LVS的功能
具备squid等专业缓存软件等的缓存功能

5.Nginx epoll模型和Apache select模型区别
Nginx使用最新的epoll(Linux2.6内核)和kqueue(freebsd)异步网络I/O模型,而Apache则使用的是传统的select模型
目前Linux下能够承受高并发访问的squid、Memcached软件都采用的是epoll模型

简单总结:
1)epoll和select 网络IO处理模型
2)epoll异步网络IO处理模型
3)select传统的网络IO模型,高并发能力弱
4)Apache则使用的是传统的select模型,Nginx使用高并发的epoll模型

6.网络IO模型概述
网络IO可以抽象成用户态和内核态之间的数据交换,一次网络数据读取操作(read),可以拆分成两个步骤:
1)网卡驱动等待数据准备好(内核态)
2)将数据从内核空间拷贝到进程空间(用户态)
根据这两个步骤处理方式不一样,我们通常把网络IO划分成阻塞IO和非阻塞IO
.1.  操作系统需要两种CPU状态
      内核态(Kernel Mode):运行操作系统程序、操作硬件
      用户态(User Mode):运行用户程序
      阻塞IO,同步模型
      非阻塞IO,异步模型

系统调用:内核态为用户态程序提供的访问接口
阻塞IO:用户调用网络IO相关的系统调用时(例:read)如果此时内核网卡还没有读取到网络数据,那么本次系统调用将会一直阻塞,直到对端系统发送的数据到达为止,如果对端一直没有发送数据,则本次调用将永远不会返回
非阻塞IO:用户调用网络IO相关的系统调用时(例:read)如果此时内核网卡还没有读取到网络数据,那么本次系统调用将会立即返回,并返回一个EAGAIN的错误码

网络IO:用户态和内核态之间的数据交换
网络IO事件:用户态和内核态之间的数据交换的事件
检测处理事件机制:网络IO模型,epoll select kquene

二:Nginx配置
1.加入全局变量
[root@web01 data]# export PATH="/data/nginx/sbin/:$PATH"                        #临时生效
将上面的变量加入到/etc/profile最下面一行中,然后在source /etc/profile       #永久生效
知识扩展:使用systemctl控制nginx
tar包安装的
[root@web02 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/data/apd/nginx/nginx.pid
ExecStart=/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /data/apd/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /data/apd/nginx/nginx.pid)"

[Install]
WantedBy=multi-user.target

[root@web01 data]# pkill nginx
[root@web01 data]# systemctl start nginx
[root@web01 data]# systemctl enable nginx

#描述从浏览器打开http://dddeeee.add.com地址回车发送请求看到页面的过程
#1.DNS解析
#检查客户端的hosts和客户端缓存,
#2.tcp三次握手建立连接
#3.http请求报文阶段
#4.整个网站架构内部请求
#5.http响应报文
#6.建立私有连接超时之后,tcp四次断开

2.调整yum源优先级
Nginx的安装包在epel中也有,但比较旧,已经把Nginx的源加入/etc/yum.repos.d/nginx.repo,但是yum安装的时候,会选择epel里的Nginx软件包,而不是先择Nginx源里的
解决方法:使用yum的yum-plugin-priorities
确认配置文件内容
cat /etc/yum/pluginconf.d/priorities.conf
[main]
enabled=1
在/etc/yum.repos.d/nginx.repo中的每个节点加入
priority=1
priority越小表示优先级越高

3.nginx主进程和子进程
Nginx master进程是nginx的主进程,主要管理和监控worker process子进程
worker process子进程的个数是由cpu的个数决定的
访问请求都是由worker process来处理的
在配置文件中这一行输入
worker_processes 8;

4.Nginx所有配置文件信息(yum安装)
rpm -ql nginx                                #查询nginx目录结构
/etc/logrotate.d/nginx                   #nginx日志切割
/etc/nginx/mime.types                  #Nginx所支持的文件类型
/etc/nginx/modules                       #Nginx的模块路径指向/usr/lib64/nginx/modules
######
和动态程序交互的进程配置
/etc/nginx/fastcgi_params            #fastcgi参数,配合和PHP-fcgi联系配置
/etc/nginx/uwsgi_params              #uwsgi参数,配合动态服务python配置
########
/usr/sbin/nginx                               #可执行文件,根据源码编译多模块,命令里有模块就可以添加模块(在任意地方编译第三方模块命令,拷贝到这里)
/usr/sbin/nginx-debug                    #可执行文件,加载日志更多包括debug,notice日志,最好少用,io磁盘承受不住

/usr/share/nginx
/usr/share/nginx/html                      #默认站点目录
/usr/share/nginx/html/50x.html       #报错5xx,重定向的页面
/usr/share/nginx/html/index.html    #默认的首页,如果删除会报403 ,修改文件权限也是报403
/var/cache/nginx                             #缓存目录
/var/log/nginx                                  #日志目录

Web服务403错误原因
1).没有首页文件,index.html,index.php(没有开目录浏览功能)
2).站点目录以及首页文件权限


nginx日志切割配置:由logrotate,rsyslog工具进行切割
Nginx日志切割解决方案:访问日志要一天一个
[root@web02 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
           daily
           dateext        #时间后缀
           missingok
           rotate 52
           #compress #自动打包,压缩:
           delaycompress
           notifempty
           create 640 nginx adm
           sharedscripts
           postrotate
                           if [ -f /var/run/nginx.pid ]; then
                           kill -USR1 `cat /var/run/nginx.pid`
                           fi
           endscript
}
在/etc/logrotate.conf文件中进行配置切割
[root@web02 ~]# egrep -v '^$|#' /etc/logrotate.conf
weekly
rotate 4
create
dateext
include /etc/logrotate.d
/var/log/wtmp {
       monthly
       create 0664 root utmp
       minsize 1M
       rotate 1
}
/var/log/btmp {
       missingok
       monthly
       create 0600 root utmp
       rotate 1
}

使用logrotate,rsyslog工具进行切割(tar包安装,找到自己的日志路径,然后自己配置/etc/logrotate.d/nginx文件上面有模板需要修改路径和用户)
[root@web01 conf]# logrotate -f /etc/logrotate.d/nginx
[root@web01 conf]# ll /data/logs/nginx/
总用量 44
-rw-r----- 1 www www 0 12月 3 14:02 access.log
-rw-r--r-- 1 www www 27145 12月 2 18:19 access.log-20211203
-rw-r----- 1 www www 0 12月 3 14:02 error.log
-rw-r--r-- 1 www www 16208 12月 2 18:19 error.log-20211203
在/data/logs/nginx/下产生error.log.1和access.log.1配置成功

5.让logrodate每天进行一次滚动,在crontab中添加一行定时脚本
[root@web01 conf]# crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
每天23点59分进行日志滚动
配置之后启动定时任务
[root@web01 ~]# systemctl reload crond
[root@web01 ~]# systemctl status crond

6.nginx模块
ngx_http_core_module                     包括一些核心的http参数模块,对应Nginx的配置为http区块部分
ngx_http_access_module                 访问控制模块,用来控制网站用户对Nginx的访问
ngx_http_gzip_module                      压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_module fastcgi       模块,和动态相关的模块,例如:PHP
ngx_http_proxy_module proxy          代理模块
ngx_http_upstream_module              负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_module                  URL地址重写模块
ngx_http_limit_conn_module             限制用户并发连接数及请求模块
ngx_http_limit_req_module                根据定义的key限制Nginx请求过程的速率
ngx_http_log_module                         访问日志模块,以指定的格式记录Nginx客户访问日志等消息
ngx_http_auth_basic_module             Web认证模块,设置Web用户通过账号密码访问Nginx
ngx_http_ssl_module                          ssl模块,用于加密的http连接,如https
ngx_http_stub_status_module            记录Nginx基本访问状态信息等的模块

7.中文乱码处理charset utf-8
[root@web02 ceshi]# cat /etc/nginx/conf.d/test.conf
server {
         listen                80;
         server_name    localhost;
         location / {
                  root               /usr/share/nginx/html;
                  index            index.html index.htm;
                  charset utf-8;
         }

          location /ceshi {
                   alias              /usr/share/nginx/html/ceshi;
                   index             index.html index.htm *.html;
                   charset utf-8;
          }

#        location /ceshi2/ {
#                  alias               /usr/share/nginx/html;
#                  index              index.html index.htm *.html;
#                  charset utf-8;
#        } 
}

知识扩展:root和alias的区别
root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径
不同的是root处理结果是root+location,而alias的处理结果是使用alias路径替换掉location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用"/"结束,否则会找不到文件的,而root则可有可无。
所以,一般情况下,在nginx配置中
(1)在location /中配置root目录
(2)在location /ceshi/中配置alias目录
案例:
        location / {
                root       /usr/share/nginx/html;
                index     index.html index.htm;
                charset utf-8;
        }
         location /ceshi/ {
                 alias       /usr/share/nginx/html/ceshi/;
                 index      index.html index.htm *.html;
                 charset   utf-8;
        }

三:Nginx多域名服务
1.网页分类
首页:     www.jd.com
二级页: diannao.jd.com
               channel.jd.com
内容页:https://item.jd.com/100017178122.html
apache虚拟机主机包含在<VirtualHost></VirtualHost>内
而Nginx软件则使用一个server{}标签来标示一个虚拟机主机

2.为什么要用虚拟主机?
(1)一个nginx主进程,指定一个配置文件,配置文件里有多个虚拟主机
(2)如果不用虚拟主机,那么一个域名就要对应一个服务器,浪费资源
(3)多实例:多个nginx进程,每个nginx进程,指定不同得配置文件(目录、代码、域名也不同)


3.虚拟主机分类
基于IP转发
以不同的IP,来区分不同虚拟主机,放在一个nginx服务上,能够让用户有序访问
在/etc/nginx/conf.d创建子配置文件ip.conf
[root@Web conf.d]# cat ip.conf
server {
              listen           192.168.20.12;                #默认为80
              root              /data/nginx/ip;                 #网页根路径
              index            index.html;
            }

基于端口转发
以不同的端口,来区分多个虚拟主机,放在一个nginx服务上,能够让用户有序访问
生产用途:
1)不对外提供访问的服务,网站的后台,测试环境
2)应用,API接口(192.168.20.12:8091)
3)所有的网站也用特殊端口,前端有负载均衡(80),负载均衡下面的节点是什么端口无所谓

http://www.ett.com/admin/ ####安全风险
思路:后台单独用开启Web服务器
1)不做域名解析 2)不用80端口 3)不配公网 4)VPN拨号,内网访问
在/etc/nginx/conf.d创建子配置文件port.conf
[root@Web conf.d]# cat port.conf
server{
            listen              81;                                 #端口设置81
            root               /data/nginx/port/;            #网页根路径
            index             index.html;
          }
server{
           listen              82;                                  #端口设置82
           root                /data/nginx/port2/;          #网页根路径
           index              index.html;
           }

基于域名转发
以不同的域名,来区分不同虚拟主机,放在一个nginx服务上,能够让用户有序访问
用途:给企业提供正常的网站服务
[root@web01 ceshi]# cat /data/prog/nginx-1.20.1/conf/conf.d/test.ett.com.conf
server {
           listen                  81;
           server_name     test.ett.com;
           location / {
           root          /data/prog/nginx-1.20.1/html/test;
           index        index.html index.htm;
            }
}
server {
           listen                   81;
           server_name       ceshi.ett.com;
           location / {
           root            /data/prog/nginx-1.20.1/html/ceshi;
           index          index.html index.htm;
            }
}

4.客户端访问虚拟主机原理
(1)启动nginx,根据配置文件的读取来监听本地所有网卡上对80端口的请求
(2)DNS解析过程
PC访问test.ett.com 先找本地的DNS缓存LDNS LDNS去找根服务器 根服务器有配置的顶级域名com 根服务器将com反馈给LDNS lDNS再去找com的www解析记录 将ett.com的解析反馈给LDNS ett.com就是授权DNS,然后运维人员事先购买域名ett.com域名
解析ett.com A 192.168.20.11 然后将解析记录反馈给LDNS LDNS在本地做完缓存然后将缓存反馈给PC PC去访问192.168.20.11ech0网卡建立连接
(3)建立TCP三次握手
(4)发送http请求ip + 端口
(5)Nginx服务端处理请求
(5.1)监听到客户端eth0网卡的端口请求
(5.2)读取接收到的http请求报文信息
(5.3)读取Nginx配置文件的server标签
(5.4)先匹配server标签中请求的端口号
(5.5)相同端口再匹配server_name内指定的域名(和请求头里的host字段比对)
(5.6)把对应域名下面站点目录下的用户请求的URL首页文件(index首页文件(index.xx)发给客户端
(5.7)如果没有匹配到域名,就把排在第一个顺序server标签对应内容发给客户端
(6)匹配Server标签的端口号,然后根据读到的HOST字段匹配server标签中的域名
(7)把域名下面对应站点目录里的URL对应的文件内容返回给客户端
(8)tcp连接断开(4次挥手)
(9)浏览器渲染,缓存

四:Nginx多实例
简单理解:一个nginx master进程对应一个实例
运行一个实例
/usr/sbin/nginx -c /etc/nginx/nginx1.conf

运行多个实例
/usr/sbin/nginx -c /etc/nginx/nginx2.conf
/usr/sbin/nginx -c /etc/nginx/nginx3.conf

1.为什么要用多实例
把业务区分开
运行www
/usr/sbin/nginx -c /etc/nginx/nginx1.conf
运行bat
/usr/sbin/nginx -c /etc/nginx/nginx2.conf
运行base
/usr/sbin/nginx -c /etc/nginx/nginx3.conf

2.上百项目web分用户解决方案
(1)添加两个普通用户
[root@web02 ~]# useradd zuma && echo 123456|passwd --stdin zuma
[root@web02 ~]# useradd inca && echo 123456|passwd --stdin inca
[root@web02 zuma]# tree
├── conf            #主配置文件
│   └── extra     #虚拟机主机目录
├── html           #zuma站点目录
└── log             #日志目录

[root@web02 inca]# tree
├── conf            #主配置文件
│   └── extra     #虚拟机主机目录
├── html            #zuma站点目录
└── log              #日志目录

(2)配置Nginx配置文件
#用户zuma主配置文件
user     zuma;
worker_processes 2;
error_log           /home/zuma/logs/error.log error;
pid                     /home/zuma/logs/nginx.pid;
events {
       worker_connections     1024;
}
http {
       include               mime.types;
       default_type       application/octet-stream;
       log_format      main          '$remote_addr - $remote_user [$time_local] "$request" '
                                                '$status $body_bytes_sent "$http_referer" '
                                               '"$http_user_agent" "$http_x_forwarded_for"';
       sendfile                     on;
       keepalive_timeout    65;
       include        /home/zuma/conf/conf.d/*.conf;
       charset utf-8;
}
#用户zuma子配置文件
server {
          listen                8081;
          server_name    test.ett.com;
          location /test {
          root       /home/zuma/html/test;
          index     index.html index.htm;
           }
          access_log /home/zuma/logs/access.log main;
}

#用户inca主配置文件
user      inca;
worker_processes 2;
error_log     /home/inca/logs/error.log error;
pid              /home/inca/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
        include                  mime.types;
        default_type          application/octet-stream;
        log_format main    '$remote_addr - $remote_user [$time_local] "$request" '
                                      '$status $body_bytes_sent "$http_referer" '
                                     '"$http_user_agent" "$http_x_forwarded_for"';
         sendfile           on;
         keepalive_timeout    65;
         include       /home/inca/conf/conf.d/*.conf;
         charset utf-8;
}
#用户inca子配置文件
server {
          listen                   8082; 
          server_name      ceshi.ett.com;
          location /ceshi {
                   root      /home/inca/html/ceshi;
                   index    index.html index.htm;
}
           access_log /home/inca/logs/access.log main;
}

(3)测试和启动命令
测试命令
[zuma@web01 test]$ /data/prog/nginx-1.20.1/sbin/nginx -c /home/zuma/conf/nginx.conf -t
nginx: [alert] could not open error log file: open() "/data/logs/nginx/error.log" failed (13: Permission denied)
2021/12/06 16:35:14 [warn] 8526#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/zuma/conf/nginx.conf:2
nginx: the configuration file /home/zuma/conf/nginx.conf syntax is ok
nginx: configuration file /home/zuma/conf/nginx.conf test is successful
启动命令
[zuma@web01 test]$ /data/prog/nginx-1.20.1/sbin/nginx -c /home/zuma/conf/nginx.conf &> /dev/null

五:虚拟主机特殊功能优化
恶意域名解析
1.什么是恶意域名解析
www.jd.com A 1.1.1.1
解决办法:https://www.cnblogs.com/dadonggg/p/8398112.html

Nginx服务
定义一个默认的空主机名,禁止其访问,需要通过的域名一定要在其他server配置。
server {
             listen   80        default;
             server_name "";
             return 444;
}
或者

server {
              listen    80       default;
              server_name _;
              return 444;
}

原文地址:https://www.cnblogs.com/zgqbky/p/15656073.html