day46 nginx的配置文件

day46 nginx的配置的文件

nginx 的安装

配置yum的源一键下载即可

1. 配置官网yum源,一键安装即可
-----------》这一步如果配置啦阿里云的官方的第三方的库就过
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[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
EOF
   
2.清空yum源,安装最新版nginx
[root@web-8 /etc/yum.repos.d]#yum clean all

[root@web-8 /etc/yum.repos.d]#yum install nginx -y

3.查看PATH变量
[root@web-8 /etc/yum.repos.d]#which nginx
/usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#ll /usr/sbin/nginx
-rwxr-xr-x 1 root root 1377720 Nov 16  2021 /usr/sbin/nginx


[root@web-8 /etc/yum.repos.d]#nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --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/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'


nginx 管理的命令

nginx -t  # 检测nginx.conf语法
nginx -s  reload  # 重新读取nginx.conf
nginx -s stop   # 停止nginx  kill -15 nginx

nginx  # 默认是直接运行,前提是当前机器没运行nginx

#你通过yum安装的nginx请你用systemctl去管理


# 不能多次执行nginx二进制命令
[root@web-8 ~]#
[root@web-8 ~]#nginx
[root@web-8 ~]#nginx # 会报错


# nginx -s reload ,会发生什么

nginx -s reload是给master进程发信号,重新读取配置信息,导致worker重新生成,因此worker-pid发生了变化

但是master进程id不带变化的(包工头,一直没变,更换了手底下的干活的工人)
================================
配置文件变化,就好比 合同变化了(包工头还是他,但是工人更换了一批)



===========
只有你restart的时候,包工头,也会被更换

你只能先停止, 再重启
nginx -s stop # 不会报错的

==============================

如果出现如下错误,如何解决,其实是通过pid,管理nginx的进程
 ~]#nginx -s stop
nginx: [error] invalid PID number "" in "/var/run/nginx.pid"
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#cat /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#ps -ef |grep nginx
root       3599      1  0 16:10 ?        00:00:00 nginx: master process nginx
nginx      3628   3599  0 16:12 ?        00:00:00 nginx: worker process
root       3677   3434  0 16:19 pts/0    00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#echo 3599 > /var/run/nginx.pid 
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#nginx -s stop
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#!ps
ps -ef |grep nginx
root       3686   3434  0 16:19 pts/0    00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]## 看懂了扣 6 不懂7 
[root@web-8 ~]#



# 明确,现在用systemctl去管理nginx了
[root@web-8 ~]#systemctl start nginx


查看状态,reload, restart nginx,查看进程id号

[root@web-8 ~]#systemctl status nginx

[root@web-8 ~]#systemctl reload nginx  # worker变化,master不变

[root@web-8 ~]#systemctl restart nginx  # 整个nginx进程变化

# 用什么命令启动的,就用什么方式去管理该进程


 


nginx 配置文件的解释

  • 安装完后就是修改配置文件 后续的功能围绕着nginx的配置文件生效
  • 看懂配置文件是很重要的,对于运维来说达到手写nginx 配置文件才算是合格的。

从官网上yum默认安装的nginx.conf

/etc/nginx/nginx.conf 

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        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 {
        }
    }

# 对于配置文件的解释
1.nginx.conf 主要的配置的文件
2.nginx支持include参数,导入外部文件用于优化的配置,让配置问价看这更加的清晰。


第一个部分是全局的配置/etc/nginx.conf 中的全局的配置。核心的模块。设置nginx的运行的用户,worker的数量,日志的参数设置
在全局中的日志的参数,会对后续的所有的虚拟机产生影响。

第二个部分 是关于nginx的性能的设置,tcp的连接的数量,也就是设置多少个进程。

第三部分 nginx的核心的部分都在这个http{}这个区域

这是一个重点----要求背诵

# nginx 的配置的语法
server{}虚拟主机标签,必须写在http{}的区域中,即使是include这个参数也得写在http{}

1.http{}中允许嵌套多个server{}
server内部允许有多个location{}

http{  server {   location }  } ------关系

2.http{} ----标签用于解决用户的请求和响应整体的功能。

3,server{} ----- 用于响应某一个具体的网站(域名)

4.location{}  ------  用于匹配网站的具体的url的路径。



posted @ 2025-04-04 22:50  国家一级冲浪yzk  阅读(26)  评论(0)    收藏  举报