分布式 - 服务器Nginx:一小时入门系列之静态网页配置

文章目录

      • 1. 静态文件配置
      • 2. nginx listen 命令解析
      • 3. nginx server_name 命令解析
      • 4. nginx server 端口重复
      • 5. nginx location 命令

1. 静态文件配置

在 /home 文件下配置一个静态的AdminLTE后台管理系统:

[root@nginx-dev conf.d]# cd /home
[root@nginx-dev home]# ls
AdminLTE-3.2.0  apache-tomcat-8.5.81  apps  sql
[root@nginx-dev home]# cd AdminLTE-3.2.0/
[root@nginx-dev AdminLTE-3.2.0]# ll
total 1412
drwxr-xr-x.  6 root root      53 Feb  8  2022 build
-rwxr-xr-x.  1 root root    3173 Feb  8  2022 CODE_OF_CONDUCT.md
-rwxr-xr-x.  1 root root     469 Feb  8  2022 composer.json
drwxr-xr-x.  5 root root      38 Feb  8  2022 dist
-rwxr-xr-x.  1 root root     169 Feb  8  2022 docker-compose.yml
-rwxr-xr-x.  1 root root     171 Feb  8  2022 Dockerfile
drwxr-xr-x.  7 root root    4096 Feb  8  2022 docs
-rwxr-xr-x.  1 root root   31161 Feb  8  2022 iframe-dark.html
-rwxr-xr-x.  1 root root   31521 Feb  8  2022 iframe.html
-rwxr-xr-x.  1 root root   72974 Feb  8  2022 index2.html
-rwxr-xr-x.  1 root root   43689 Feb  8  2022 index3.html
-rwxr-xr-x.  1 root root   61756 Feb  8  2022 index.html
-rwxr-xr-x.  1 root root    1082 Feb  8  2022 LICENSE
-rwxr-xr-x.  1 root root    6778 Feb  8  2022 package.json
-rwxr-xr-x.  1 root root 1134163 Feb  8  2022 package-lock.json
drwxr-xr-x. 10 root root     196 Feb  8  2022 pages
drwxr-xr-x. 62 root root    4096 Feb  8  2022 plugins
-rwxr-xr-x.  1 root root    6716 Feb  8  2022 README.md
-rwxr-xr-x.  1 root root   13336 Feb  8  2022 starter.html

① 下面将其配置成一个web服务:nginx 配置文件位于 /etc/nginx/nginx.conf 中,其中 include 命令会引用 /etc/nginx/conf.d 目录下所有的 .conf 文件,因次可以在conf.d文件下新建一个配置文件 8000.conf

[root@nginx-dev home]# cd /etc/nginx/conf.d/
[root@nginx-dev conf.d]# ls
admin-8000.conf  default.conf
[root@nginx-dev conf.d]# vi 8000.conf
[root@nginx-dev conf.d]# nginx -s reload

其中 8000.conf 配置文件的内容为:

server{
    listen 8000;
    server_name localhost;
    
    location / {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}

它监听本地的 8000 端口,并将所有请求都定向到 /home/AdminLTE-3.2.0 目录下的文件。如果请求的 URL 路径是 /,则会尝试返回 index.html、index2.html 或 index3.html 中的一个作为响应。这个配置文件的作用是将本地的 AdminLTE-3.2.0 网页项目部署到 Nginx 服务器上,以便通过浏览器访问。

② 在浏览器访问8000端口:http://192.168.1.9:8000/

分布式 - 服务器Nginx:一小时入门系列之静态网页配置_第1张图片

2. nginx listen 命令解析

nginx 是一款高性能的 Web 服务器和反向代理服务器,listen 是 nginx 配置文件中用于指定监听地址和端口的指令。

listen 指令的语法如下:

listen address[:port] [parameters];

其中,address 表示要监听的 IP 地址,可以是一个具体的 IP 地址,也可以是一个通配符,如 * 表示监听所有可用的 IP 地址。port 表示要监听的端口号,如果不指定,则默认为 80。

listen 指令还可以带有一些可选的参数,如下所示:

① default_server:指定该监听地址和端口为默认服务器,当客户端请求的域名在 nginx 中没有匹配的虚拟主机时,会使用该默认服务器处理请求。

② ssl:指定该监听地址和端口使用 SSL/TLS 加密协议进行通信。

③ http2:指定该监听地址和端口使用 HTTP/2 协议进行通信。

④ reuseport:启用 SO_REUSEPORT 选项,允许多个进程或线程同时监听同一个地址和端口,提高并发处理能力。

例如,以下配置指定 nginx 监听 192.168.1.100 的 80 端口,并启用 default_server 参数:

listen 192.168.1.100:80 default_server;

监听可以配置成IP端口IP+端口 :

listen 127.0.0.1:8000; 指定Nginx监听127.0.0.1的8000端口。
listen 127.0.0.1; 指定Nginx监听127.0.0.1的80端口(因为端口号未指定,默认为80)。
listen 8000; 指定Nginx监听所有可用IP地址的8000端口。
listen *:8000; 指定Nginx监听所有可用IP地址的8000端口。
listen localhost:8000; 指定Nginx监听本地主机名localhost的8000端口。

3. nginx server_name 命令解析

server_name 是 Nginx 配置文件中用于指定虚拟主机的域名或 IP 地址的指令。它用于告诉 Nginx 哪些请求应该被转发到该虚拟主机,主要用于区分,可以随便起。也可以使用变量$hostname配置成主机名。

server_name 指令可以在 http、server 和 location 块中使用。在 http 块中使用 server_name 指令可以设置默认的虚拟主机,而在 server 块中使用 server_name 指令可以设置特定的虚拟主机。

server_name 指令可以指定一个或多个域名或 IP 地址,多个域名或 IP 地址之间用空格或逗号分隔。例如:

server {
    listen 80;
    server_name example.com www.example.com;
    ...
}

或者配置成域名:example.org www.example.org *.example.org

上面的配置指定了两个域名 example.com 和 www.example.com,当请求的主机名为这两个域名之一时,Nginx 将会将请求转发到该虚拟主机。

4. nginx server 端口重复

如果多个server的端口重复,那么根据域名或者主机名去匹配 server_name 进行选择。

① 查看 /etc/nginx/conf.d 目录下的 default.conf 文件,可以看到,配置文件监听80端口,服务器名为localhost

[root@nginx-dev conf.d]# cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

② 修改 /etc/nginx/conf.d 目录下的 8000.conf 文件,将监听端口页修改为80,重启 nginx 会提醒 conflicting server name “localhost” on 0.0.0.0:80, ignored

[root@nginx-dev conf.d]# vi /etc/nginx/conf.d/8000.conf
server{
    listen 80;
    server_name localhost;

    location / {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}
[root@nginx-dev conf.d]# nginx -s reload
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

③ 重新修改 /etc/nginx/conf.d 目录下的 8000.conf 文件,将 server_name 修改为 nginx-dev

[root@nginx-dev conf.d]# vi /etc/nginx/conf.d/8000.conf
server{
    listen 80;
    server_name nginx-dev;

    location / {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}

④ 如果访问 curl http://localhost:80,那么响应的是 /usr/share/nginx/html 目录下的首页文件:

# [root@nginx-dev conf.d]# curl localhost:80 会访问这个
server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

⑤ 如果访问 curl http://nginx-dev:80,那么响应的是 /home/AdminLTE-3.2.0 目录下的首页文件:

# [root@nginx-dev html]# curl http://nginx-dev:80 会访问这个
server{
    listen 80;
    server_name nginx-dev;#主机名

    location / {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}

5. nginx location 命令

/请求指向 root 目录,location 总是从/目录开始匹配,如果有子目录,例如/css,他会指向/static/css

location /css {
  	root /static;
}

① 修改 /etc/nginx/conf.d 目录下的 8000.conf 文件:

[root@nginx-dev html]# nginx -s reload
[root@nginx-dev html]# cat /etc/nginx/conf.d/8000.conf
server{
    listen 8000;
    server_name localhost;

    location /admin {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}

/请求总是指向 root 目录,/admin 目录则指向的是root目录下的admin目录,即为 /home/AdminLTE-3.2.0/admin。

在浏览器访问 http://192.168.1.9:8000/admin :报错404

分布式 - 服务器Nginx:一小时入门系列之静态网页配置_第2张图片

查看nginx错误日志:

[root@nginx-dev nginx]# tail -f /var/log/nginx/*.log
==> /var/log/nginx/access.log <==
192.168.1.10 - - [06/Aug/2023:22:40:38 +0800] "GET /dist/img/user1-128x128.jpg HTTP/1.1" 200 2750 "http://192.168.1.9:8000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:22:40:38 +0800] "GET /plugins/fontawesome-free/webfonts/fa-solid-900.woff2 HTTP/1.1" 200 78268 "http://192.168.1.9:8000/plugins/fontawesome-free/css/all.min.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:22:40:38 +0800] "GET /plugins/fontawesome-free/webfonts/fa-regular-400.woff2 HTTP/1.1" 200 13224 "http://192.168.1.9:8000/plugins/fontawesome-free/css/all.min.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
127.0.0.1 - - [06/Aug/2023:22:59:36 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
127.0.0.1 - - [06/Aug/2023:23:01:16 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:01:58 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:02:46 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:03:43 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
127.0.0.1 - - [06/Aug/2023:23:03:52 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.1.10 - - [06/Aug/2023:23:15:09 +0800] "GET /admin HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"

==> /var/log/nginx/error.log <==
2023/08/06 23:14:21 [notice] 3869#3869: exit
2023/08/06 23:14:21 [notice] 3870#3870: exiting
2023/08/06 23:14:21 [notice] 3870#3870: exit
2023/08/06 23:14:21 [notice] 2483#2483: signal 17 (SIGCHLD) received from 3869
2023/08/06 23:14:21 [notice] 2483#2483: worker process 3869 exited with code 0
2023/08/06 23:14:21 [notice] 2483#2483: signal 29 (SIGIO) received
2023/08/06 23:14:21 [notice] 2483#2483: signal 17 (SIGCHLD) received from 3870
2023/08/06 23:14:21 [notice] 2483#2483: worker process 3870 exited with code 0
2023/08/06 23:14:21 [notice] 2483#2483: signal 29 (SIGIO) received
2023/08/06 23:15:09 [error] 3897#3897: *26 open() "/home/AdminLTE-3.2.0/admin" failed (2: No such file or directory), client: 192.168.1.10, server: localhost, request: "GET /admin HTTP/1.1", host: "192.168.1.9:8000"

可以看到报错信息为 /home/AdminLTE-3.2.0/admin 这个目录不存在。

② 修改 /etc/nginx/conf.d 目录下的 8000.conf 文件:

[root@nginx-dev nginx]# cat /etc/nginx/conf.d/8000.conf
server{
    listen 8000;
    server_name localhost;

    location / {
        root /home/AdminLTE-3.2.0;
        index index.html index2.html index3.html;
    }
}

在浏览器访问 http://192.168.1.9:8000/a。同样报错404:

在这里插入图片描述

查看nginx错误日志:

[root@nginx-dev nginx]# tail -f /var/log/nginx/*.log
==> /var/log/nginx/access.log <==
127.0.0.1 - - [06/Aug/2023:23:01:16 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:01:58 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:02:46 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
192.168.1.9 - - [06/Aug/2023:23:03:43 +0800] "GET / HTTP/1.1" 200 61756 "-" "curl/7.29.0" "-"
127.0.0.1 - - [06/Aug/2023:23:03:52 +0800] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
192.168.1.10 - - [06/Aug/2023:23:15:09 +0800] "GET /admin HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:23:19:48 +0800] "GET /admin HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:23:19:51 +0800] "GET /admin HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:23:19:54 +0800] "GET / HTTP/1.1" 200 61756 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"
192.168.1.10 - - [06/Aug/2023:23:20:05 +0800] "GET /a HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0" "-"

==> /var/log/nginx/error.log <==
2023/08/06 23:19:44 [notice] 3897#3897: exit
2023/08/06 23:19:44 [notice] 2483#2483: signal 17 (SIGCHLD) received from 3897
2023/08/06 23:19:44 [notice] 2483#2483: worker process 3897 exited with code 0
2023/08/06 23:19:44 [notice] 2483#2483: signal 29 (SIGIO) received
2023/08/06 23:19:44 [notice] 2483#2483: signal 17 (SIGCHLD) received from 3898
2023/08/06 23:19:44 [notice] 2483#2483: worker process 3898 exited with code 0
2023/08/06 23:19:44 [notice] 2483#2483: signal 29 (SIGIO) received
2023/08/06 23:20:05 [error] 3906#3906: *28 open() "/home/AdminLTE-3.2.0/a" failed (2: No such file or directory), client: 192.168.1.10, server: localhost, request: "GET /a HTTP/1.1", host: "192.168.1.9:8000"

可以看到报错信息为 /home/AdminLTE-3.2.0/a 这个目录不存在。

你可能感兴趣的:(【分布式-服务器Nginx】,分布式,服务器,nginx)