Web服务器之Nginx 一、介绍 Nginx是一款高性能的Web服务器,广泛用于网站搭建。
二、安装 有好多种安装方法,安装包安装、源码编译安装、配置 YUM APT 官方源安装。
2.1 配置官方APT安装 进入官网 ,点击安装 ,选择Linux Package ,选择对应的Ubuntu系统 Debian系统 ,按照提示一步一步来。
按照先决条件,依赖包
1 sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
导入官方nginx签名密钥,以便apt能够沿着软件包的真实性
1 2 curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
验证下载的文件是否含有正确的密钥
1 gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
输出应该带有完整的指纹。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 root@sg-oracle-2c12g-01:~# gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg pub rsa4096 2024-05-29 [SC] 8540A6F18833A80E9C1653A42FD21310B49F6B46 uid nginx signing key <signing-key-2@nginx.com> pub rsa2048 2011-08-19 [SC] [expires: 2027-05-24] 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 uid nginx signing key <signing-key@nginx.com> pub rsa4096 2024-05-29 [SC] 9E9BE90EACBCDE69FE9B204CBCDCD8A38D88A2B3 uid nginx signing key <signing-key-3@nginx.com> root@sg-oracle-2c12g-01:~#
和下面的官方签名密钥保持一致。
1 2 3 pub rsa2048 2011-08-19 [SC] [expires: 2027-05-24] 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 uid nginx signing key <signing-key@nginx.com>
为稳定版 nginx 设置仓库
1 2 3 echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
主线版本的nginx 仓库
1 2 3 echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
优先使用 nginx 官网提供的软件仓库,而不是ubuntu 或 debian 提供的仓库。
1 2 echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \ | sudo tee /etc/apt/preferences.d/99nginx
安装
1 2 sudo apt updatesudo apt install nginx
启动
1 systemctl enable --now nginx
2.2 配置官方YUM 安装 进入官网 ,点击安装 ,选择Linux Package ,选择对应的RHEL红帽系 ,按照提示一步一步来。
安装依赖,yum-utils
1 sudo yum install yum-utils
编辑文件/etc/yum.repos.d/nginx.repo
1 vim /etc/yum.repos.d/nginx.repo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [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 module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever /$basearch / gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
默认使用稳定版本,如需切换到主线版本
1 sudo yum-config-manager --enable nginx-mainline
安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 [root@centos9 ~]# sudo yum install nginx nginx stable repo 22 kB/s | 54 kB 00:02 Dependencies resolved. =================================================================================================== Package Architecture Version Repository Size ================================================================================================== Installing: nginx x86_64 2:1.28.0-1.el9.ngx nginx-stable 1.0 M Transaction Summary ================================================================================================== Install 1 Package Total download size: 1.0 M Installed size: 3.4 M Is this ok [y/N]: y Downloading Packages: nginx-1.28.0-1.el9.ngx.x86_64.rpm 132 kB/s | 1.0 MB 00:07 ------------------------------------------------------------------------------------------------ Total 132 kB/s | 1.0 MB 00:07 nginx stable repo 9.8 kB/s | 12 kB 00:01 Importing GPG key 0xB49F6B46: Userid : "nginx signing key <signing-key-2@nginx.com>" Fingerprint: 8540 A6F1 8833 A80E 9C16 53A4 2FD2 1310 B49F 6B46 From : https://nginx.org/keys/nginx_signing.key Is this ok [y/N]: y Key imported successfully Importing GPG key 0x7BD9BF62: Userid : "nginx signing key <signing-key@nginx.com>" Fingerprint: 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 From : https://nginx.org/keys/nginx_signing.key Is this ok [y/N]: y Key imported successfully Importing GPG key 0x8D88A2B3: Userid : "nginx signing key <signing-key-3@nginx.com>" Fingerprint: 9E9B E90E ACBC DE69 FE9B 204C BCDC D8A3 8D88 A2B3 From : https://nginx.org/keys/nginx_signing.key Is this ok [y/N]: y Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: nginx-2:1.28.0-1.el9.ngx.x86_64 1/1 Installing : nginx-2:1.28.0-1.el9.ngx.x86_64 1/1 Running scriptlet: nginx-2:1.28.0-1.el9.ngx.x86_64 1/1 ---------------------------------------------------------------------- Thanks for using nginx! Please find the official documentation for nginx here: * https://nginx.org/en/docs/ Please subscribe to nginx-announce mailing list to get the most important news about nginx: * https://nginx.org/en/support.html Commercial subscriptions for nginx are available on: * https://nginx.com/products/ ---------------------------------------------------------------------- Verifying : nginx-2:1.28.0-1.el9.ngx.x86_64 1/1 Installed: nginx-2:1.28.0-1.el9.ngx.x86_64 Complete! [root@centos9 ~]#
启动
1 systemctl enable --now nginx
2.3 验证启动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 root@sg-oracle-2c12g-01:~# netstat -tlnp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20400/nginx: master tcp6 0 0 :::80 :::* LISTEN 20400/nginx: master root@sg-oracle-2c12g-01:~# root@sg-oracle-2c12g-01:~# 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 Sun 2025-10-12 07:03:36 UTC; 14min ago Docs: man:nginx(8) Main PID: 20400 (nginx) Tasks: 3 (limit : 14231) Memory: 3.2M CPU: 23ms CGroup: /system.slice/nginx.service ├─20400 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ├─20401 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" └─20402 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Oct 12 07:03:36 sg-oracle-2c12g-01 systemd[1]: Starting A high performance web server and a reverse proxy server... Oct 12 07:03:36 sg-oracle-2c12g-01 systemd[1]: Started A high performance web server and a reverse proxy server. root@sg-oracle-2c12g-01:~#
2.4 测试访问 需放行80端口,或关闭防火墙
1 2 3 4 5 systemctl stop firewalld systemctl stop ufw
三、基本使用 3.1 配置文件 3.1.1 整体配置文件 默认配置文件夹在 /etc/nginx,主配置文件是 nginx.conf ,它内部引用配置目录 conf.d/。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 root@sg-oracle-2c12g-01:~# ls -l /etc/nginx/ total 64 drwxr-xr-x 2 root root 4096 Oct 12 06:33 conf.d -rw-r--r-- 1 root root 1125 May 30 2023 fastcgi.conf -rw-r--r-- 1 root root 1055 May 30 2023 fastcgi_params -rw-r--r-- 1 root root 2837 May 30 2023 koi-utf -rw-r--r-- 1 root root 2223 May 30 2023 koi-win -rw-r--r-- 1 root root 3957 May 30 2023 mime.types drwxr-xr-x 2 root root 4096 Aug 22 12:46 modules-available drwxr-xr-x 2 root root 4096 Oct 12 05:41 modules-enabled -rw-r--r-- 1 root root 1447 May 30 2023 nginx.conf -rw-r--r-- 1 root root 180 May 30 2023 proxy_params -rw-r--r-- 1 root root 636 May 30 2023 scgi_params drwxr-xr-x 2 root root 4096 Oct 12 05:41 sites-available drwxr-xr-x 2 root root 4096 Oct 12 05:41 sites-enabled drwxr-xr-x 2 root root 4096 Oct 12 05:41 snippets -rw-r--r-- 1 root root 664 May 30 2023 uwsgi_params -rw-r--r-- 1 root root 3071 May 30 2023 win-utf root@sg-oracle-2c12g-01:~# [root@centos9 nginx]# ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params [root@centos9 nginx]# [root@centos7 nginx]# ls -al total 84 drwxr-xr-x 4 root root 4096 Oct 12 15:23 . drwxr-xr-x. 81 root root 8192 Oct 12 15:23 .. drwxr-xr-x 2 root root 6 Nov 11 2022 conf.d drwxr-xr-x 2 root root 6 Nov 11 2022 default.d -rw-r--r-- 1 root root 1077 Nov 11 2022 fastcgi.conf -rw-r--r-- 1 root root 1077 Nov 11 2022 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Nov 11 2022 fastcgi_params -rw-r--r-- 1 root root 1007 Nov 11 2022 fastcgi_params.default -rw-r--r-- 1 root root 2837 Nov 11 2022 koi-utf -rw-r--r-- 1 root root 2223 Nov 11 2022 koi-win -rw-r--r-- 1 root root 5231 Nov 11 2022 mime.types -rw-r--r-- 1 root root 5231 Nov 11 2022 mime.types.default -rw-r--r-- 1 root root 2336 Nov 11 2022 nginx.conf -rw-r--r-- 1 root root 2656 Nov 11 2022 nginx.conf.default -rw-r--r-- 1 root root 636 Nov 11 2022 scgi_params -rw-r--r-- 1 root root 636 Nov 11 2022 scgi_params.default -rw-r--r-- 1 root root 664 Nov 11 2022 uwsgi_params -rw-r--r-- 1 root root 664 Nov 11 2022 uwsgi_params.default -rw-r--r-- 1 root root 3610 Nov 11 2022 win-utf [root@centos7 nginx]#
3.1.2 主配置文件解读 1 egrep '^[ ]*#|^$' -v /etc/nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@centos9 conf.d]# egrep '^[ ]*#|^$' -v /etc/nginx/nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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"' ; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; } [root@centos9 conf.d]#
include /etc/nginx/conf.d/*.conf; 包含子配置文件
3.2 最小配置文件部署站点001 创建新网站,在conf.d 目录下新建子配置文件,
1 2 mkdir -p /www/wwwroot/web-server-01vim /etc/nginx/conf.d/web-server-01.conf
1 2 3 4 5 6 7 8 9 10 11 server { listen 80; server_name web-server-01.域名; access_log /var/log/nginx/web-server-01.log ; location / { root /www/wwwroot/web-server-01; index index.html; } }
1 2 3 cat > /www/wwwroot/web-server-01/index.html << EOL Hello World! EOL
访问 http://web-server-01.域名
3.3 部署方式 3.3.1 基于IP 1 2 3 4 5 6 7 8 9 10 11 server { listen 1.1.1.1:80; server_name web-server-01.域名; access_log /var/log/nginx/web-server-01.log ; location / { root /www/wwwroot/web-server-01; index index.html; } }
3.3.2 基于端口 1 2 3 4 5 6 7 8 9 10 11 server { listen 8080; server_name web-server-01.域名; access_log /var/log/nginx/web-server-01.log ; location / { root /www/wwwroot/web-server-01; index index.html; } }
3.3.3 基于域名 1 2 3 4 5 6 7 8 9 10 11 server { listen 80; server_name web-server-02.域名; access_log /var/log/nginx/web-server-01.log ; location / { root /www/wwwroot/web-server-02; index index.html; } }
四、常见配合 4.1 LNMP Linux + Nginx + MySQL/Mariadb +PHP
4.2 LAMP Linux + APache + MySQL/Mariadb +PHP
五、封面图