Nginx 简介和入门 - part1

3 篇文章 0 订阅

虽然作为1个后端程序员, 终究避不开这东西

安装Nginx

本人的测试服务器是debian , 安装过程跟ubuntu基本一样

sudo apt-get install nginx

问题是 nginx 安装后 执行文件在/usr/sbin 而不是/usr/bin 所以正常下普通用户是无法使用的。

必须切换到root 用户去配置nginx

gateman@tf-vpc0-subnet0-main-server:~$ nginx -v
-bash: nginx: command not found
gateman@tf-vpc0-subnet0-main-server:~$ sudo su -
root@tf-vpc0-subnet0-main-server:~# nginx -v
nginx version: nginx/1.18.0
root@tf-vpc0-subnet0-main-server:~# 

查看服务注册, 可以简单注册nginx.service 已经成功, 而且已启动

root@tf-vpc0-subnet0-main-server:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-01-01 12:23:06 UTC; 28min ago
       Docs: man:nginx(8)
    Process: 32767 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 32768 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 32961 (nginx)
      Tasks: 5 (limit: 19184)
     Memory: 7.0M
        CPU: 26ms
     CGroup: /system.slice/nginx.service
             ├─32961 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─32963 nginx: worker process
             ├─32964 nginx: worker process
             ├─32965 nginx: worker process
             └─32966 nginx: worker process

Jan 01 12:23:06 tf-vpc0-subnet0-main-server systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 01 12:23:06 tf-vpc0-subnet0-main-server systemd[1]: Started A high performance web server and a reverse proxy server.
root@tf-vpc0-subnet0-main-server:~# 




查看Nginx 监听的端口

root@tf-vpc0-subnet0-main-server:~# sudo lsof -i -P -n | grep LISTEN
exim4       830 Debian-exim    4u  IPv4  11734      0t0  TCP 127.0.0.1:25 (LISTEN)
exim4       830 Debian-exim    5u  IPv6  11735      0t0  TCP [::1]:25 (LISTEN)
sshd        954        root    3u  IPv4  10970      0t0  TCP *:22 (LISTEN)
sshd        954        root    4u  IPv6  10972      0t0  TCP *:22 (LISTEN)
docker-pr  1304        root    4u  IPv4  12180      0t0  TCP *:8201 (LISTEN)
docker-pr  1309        root    4u  IPv6  12732      0t0  TCP *:8201 (LISTEN)
docker-pr  1401        root    4u  IPv4  12777      0t0  TCP *:2222 (LISTEN)
docker-pr  1408        root    4u  IPv6  13466      0t0  TCP *:2222 (LISTEN)
docker-pr  1453        root    4u  IPv4  13489      0t0  TCP *:63790 (LISTEN)
docker-pr  1459        root    4u  IPv6  14429      0t0  TCP *:63790 (LISTEN)
docker-pr  1474        root    4u  IPv4  14437      0t0  TCP *:33062 (LISTEN)
docker-pr  1481        root    4u  IPv6  12269      0t0  TCP *:33062 (LISTEN)
docker-pr  1500        root    4u  IPv4  12276      0t0  TCP *:33061 (LISTEN)
docker-pr  1507        root    4u  IPv6  12279      0t0  TCP *:33061 (LISTEN)
nginx     32961        root    6u  IPv4 127458      0t0  TCP *:80 (LISTEN)
nginx     32961        root    7u  IPv6 127459      0t0  TCP *:80 (LISTEN)
nginx     32963    www-data    6u  IPv4 127458      0t0  TCP *:80 (LISTEN)
nginx     32963    www-data    7u  IPv6 127459      0t0  TCP *:80 (LISTEN)
nginx     32964    www-data    6u  IPv4 127458      0t0  TCP *:80 (LISTEN)
nginx     32964    www-data    7u  IPv6 127459      0t0  TCP *:80 (LISTEN)
nginx     32965    www-data    6u  IPv4 127458      0t0  TCP *:80 (LISTEN)
nginx     32965    www-data    7u  IPv6 127459      0t0  TCP *:80 (LISTEN)
nginx     32966    www-data    6u  IPv4 127458      0t0  TCP *:80 (LISTEN)
nginx     32966    www-data    7u  IPv6 127459      0t0  TCP *:80 (LISTEN)

如图, 就是用的80




测试ngnix主页

在这里插入图片描述



nginx 主要文件目录介绍

/etc/nginx/ – 包含了所有nginx的配置文件
/etc/nginx/nginx.conf – 主配置文件

/etc/nginx/conf.d/
在 Nginx 中,/etc/nginx/conf.d 目录用于存储自定义的配置文件片段。这些配置文件片段通常以 .conf 扩展名结尾,并包含了特定的 Nginx 配置指令和块,用于配置服务器的不同方面。

当 Nginx 启动时,它会加载主配置文件(通常为 /etc/nginx/nginx.conf),并在主配置文件中包含 /etc/nginx/conf.d 目录中的所有配置文件片段。

/etc/nginx/sites-available/
sites-available目录是用于存放可用的虚拟主机配置文件的位置。每个配置文件代表一个独立的虚拟主机或站点配置。在这个目录中的配置文件通常以.conf` 扩展名结尾,包含有关虚拟主机的配置指令和设置。

当您想要添加一个新的虚拟主机或站点时,您可以在 sites-available 目录中创建一个新的配置文件,并定义该虚拟主机的配置。这样的配置文件可以包括监听的端口、域名、SSL 证书、请求转发规则等。

/etc/nginx/sites-enabled/
sites-enabled目录是用于存放启用的虚拟主机配置的位置。在这个目录中的配置文件是sites-available目录中的配置文件的符号链接(软链接)。只有位于sites-enabled` 目录中的配置文件才会在 Nginx 启动时生效。

通过将符号链接添加到 sites-enabled 目录,您可以选择性地启用或禁用特定的虚拟主机或站点配置。这对于在多个虚拟主机之间切换或禁用某些配置非常有用,而无需删除或移动实际的配置文件。

通常,sites-available 目录用于存储所有可用的虚拟主机配置文件,而 sites-enabled 目录用于存储要在 Nginx 中启用的实际配置文件。这种组织结构使得管理和维护多个虚拟主机变得更加方便。

我们查看 /etc/nginx/sites-enabled/default 这个目录

root@tf-vpc0-subnet0-main-server:/etc/nginx# ls -lrt /etc/nginx/sites-enabled/default 
lrwxrwxrwx 1 root root 34 Jan  1 12:23 /etc/nginx/sites-enabled/default -> /etc/nginx/sites-available/default

可以见到defaut folder 就是1个软连接,
/etc/nginx/sites-enabled/default 实际上就是 /etc/nginx/sites-available/default

/var/log/nginx/
存放日志文件

/var/www/
存放nginx的静态文件, 例如 nginx 欢迎页面的html就在这里

好了, 那么到底 /etc/nginx/conf.d/ 和 /etc/nginx/sites-enabled/的区别是啥, 到底配置文件放哪里??

Nginx 中存在两种不同的配置风格(使用 /etc/nginx/sites-enabled/ 和 /etc/nginx/conf.d/ 目录)是为了提供更大的灵活性和适应不同的使用场景。

/etc/nginx/sites-enabled/ 目录风格:
这种风格通过使用符号链接的方式,将实际的虚拟主机配置文件放置在 /etc/nginx/sites-available/ 目录,然后将其符号链接到 /etc/nginx/sites-enabled/ 目录中。这种方式允许您通过创建或删除符号链接来启用或禁用特定的虚拟主机配置。

这种风格的优点是可以轻松地启用或禁用不同的虚拟主机配置,而不需要编辑主配置文件。这对于管理多个虚拟主机或进行动态配置更加方便。

/etc/nginx/conf.d/ 目录风格:
这种风格允许您直接将虚拟主机的配置文件放置在 /etc/nginx/conf.d/ 目录中,而不需要使用符号链接。每个虚拟主机的配置文件可以是独立的文件,以 .conf 为扩展名。

这种风格的优点是更加简洁和直观,您可以直接在目录中创建和管理每个虚拟主机的配置文件,而不需要创建符号链接。这对于简单的配置场景或较少的虚拟主机数量更加方便。

两种风格的选择取决于个人偏好和特定的使用案例。某些人更喜欢使用符号链接来启用或禁用虚拟主机,而另一些人更喜欢直接管理虚拟主机的配置文件。

本文会尽量用第1种风格




nginx.conf 主配置文件简单内容解释

gateman@tf-vpc0-subnet0-main-server:/etc/nginx$ cat nginx.conf 
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

其中 user www-data 代表nginx实际上会以这个用户运行

其他代码模块都是 在一些 大括号中
例如 event {} 意思如下:

worker_connections 768;:
这个指令设置每个 worker 进程可处理的最大连接数。在这个例子中,每个 worker 进程被配置为可以处理最多 768 个连接。请注意,这个值应根据服务器的负载和性能需求进行调整。

multi_accept on;(注释掉的指令):
这是一个可选的指令,用于启用或禁用 Nginx 的多连接接受机制。当启用时,每个 worker 进程可以同时接受多个连接。在此示例中,该指令被注释掉了,表示该功能被禁用。

当 multi_accept 设置为 off 时,worker_connections 的值仍然表示每个 worker 进程可以处理的最大连接数。即使 multi_accept 关闭,每个 worker 进程仍然能够同时处理多个连接,但它们将按顺序逐个接受连接。





在 http{} 中

sendfile on;:
这个指令启用了 Nginx 的 sendfile 功能。sendfile 是一种高效的文件传输机制,它允许直接从磁盘到网络套接字发送文件,而无需将文件内容先复制到用户空间。这可以提高文件传输的性能和效率。

tcp_nopush on;:
这个指令启用了 TCP 的 nopush 功能。当启用 nopush 时,Nginx 会在发送响应时尽快将数据发送给客户端,而无需等待数据缓冲区填满。这可以提高响应的实时性和传输效率。

types_hash_max_size 2048;:
这个指令设置了 Nginx 类型哈希表的最大大小。类型哈希表用于快速查找文件类型和 MIME 类型。通过增加哈希表的大小,可以提高类型查找的效率。

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

这两行就是定义这个http服务的日志位置

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

这里就是一些虚拟主机设置




配置1个 virtual host (虚拟主机)

我们在/etc/nginx/conf.d/ 下创建1个文件 binaryville1.conf

server {
        listen 80;
        server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;
        index index.html index.htm index.php;
        root  /var/www/binaryville1;
}

可以见到, 这个虚拟主机监听的端口同样是80, 这不是会跟自带主页有冲突?
是的, 要得就是这个效果

root  /var/www/binaryville1;

这一句的的意思是定义虚拟网站的根目录位置, 存放静态文件

index index.html index.htm index.php;

上面这句的就是定义index 入口的文件名(在定义的根目录下找), 优先级从左到右.

接下来用nginx -t 命令来检查语法

gateman@tf-vpc0-subnet0-main-server:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

然后用systemctl reload nginx 来apply change

gateman@tf-vpc0-subnet0-main-server:~$ sudo systemctl reload nginx
gateman@tf-vpc0-subnet0-main-server:~$ 

没有error 就代表成功

这时再打开主页, 发现主页就被覆盖了!
在这里插入图片描述




配置多个 virtual host (虚拟主机), 检查加载顺序

既然端口可以冲突
我们来做个实验

我们在/etc/nginx/conf.d/ 创建另外两个文件 binaryville0.conf 和 binaryville2.conf
而他们会执行不同的根目录

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# ls -l
total 12
-rw-r--r-- 1 root root 161 Jan  1 18:15 binaryville0.conf
-rw-r--r-- 1 root root 161 Jan  1 17:38 binaryville1.conf
-rw-r--r-- 1 root root 161 Jan  1 18:15 binaryville2.conf
root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville*
server {
        listen 80;
        server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;
        index index.html index.htm index.php;
        root  /var/www/binaryville0;
}
server {
        listen 80;
        server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;
        index index.html index.htm index.php;
        root  /var/www/binaryville1;
}
server {
        listen 80;
        server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;
        index index.html index.htm index.php;
        root  /var/www/binaryville2;
}
root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# 

而在他们各自的根目录下, index.html 的内容有区别, 可以区分是哪个目录的文件被加载

gateman@tf-vpc0-subnet0-main-server:~$ sudo su -
root@tf-vpc0-subnet0-main-server:~# cd /var/www
root@tf-vpc0-subnet0-main-server:/var/www# ls
binaryville0  binaryville1  binaryville2  html
root@tf-vpc0-subnet0-main-server:/var/www# tree
.
├── binaryville0
│   └── index.html
├── binaryville1
│   └── index.html
├── binaryville2
│   └── index.html
└── html
    └── index.nginx-debian.html

4 directories, 4 files
root@tf-vpc0-subnet0-main-server:/var/www# cat binaryville*/index*
binaryville0 - site comming soon!
binaryville1 - site comming soon!
binaryville2 - site comming soon!

猜猜最终谁的会被加载?

答案是binaryville0.conf
在这里插入图片描述

实际上,Nginx 在加载配置文件时会按照如下顺序进行处理:

首先,Nginx 会加载 nginx.conf 主配置文件。
然后,Nginx 会加载 /etc/nginx/conf.d/ 目录中的配置文件。它会按照文件名的字母顺序加载这些文件。
在当前情况下,根据文件名的字母顺序,Nginx 会按照以下顺序加载这三个文件:binaryville0.conf、binaryville1.conf 和 binaryville2.conf。

由于这些文件的内容相同(根据您提供的信息文件大小相同),最后加载的 binaryville2.conf 不会覆盖先前加载的文件内容。

*也就是上, 多个配置文件下, 也是最左优先的, 后加载的相同配置不会覆盖已经加载的!




配置server_name , 根据域名区分配置

其实我配置两个域名来指向同1台server
jp-gcp-vms.xyz
jp-gcp-vms.cloud

然后我改下配置文件

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville*
server {
        listen 80;
        server_name jp-gcp-vms.xyz www.jp-gcp-vms.xyz;
        index index.html index.htm index.php;
        root  /var/www/binaryville0;
}
server {
        listen 80;
        server_name 34.39.2.90;
        index index.html index.htm index.php;
        root  /var/www/binaryville1;
}
server {
        listen 80;
        server_name jp-gcp-vms.cloud www.jp-gcp-vms.cloud;
        index index.html index.htm index.php;
        root  /var/www/binaryville2;
}
root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# 

其中binaryville0 用域名1, binaryville1 用ip , binaryville0 用域名2
实际效果
在这里插入图片描述
果然我们可以用server_name 来用同1个端口区分不同的虚拟主机!

当然实际上开发用不同端口区分也可以!




配置default_server

假如3个虚拟主机配置的server_name 都匹配不上?

这种情况下 用户打开的就是binaryville0主页了, 最左原则!

而且我们也可以用 default_server 来强制 某个虚拟主机在server_name 都匹配不上时候被选择。
例如:

root@tf-vpc0-subnet0-main-server:/etc/nginx/conf.d# cat binaryville1.conf 
server {
        listen 80 default_server;
        server_name 34.39.2.90;
        index index.html index.htm index.php;
        root  /var/www/binaryville1;
}

如果 多个配置都有default_server? 还是最左原则!

后面的内容有时间再写

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nvd11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值