文章目录
-
-
- [1. Nginx 环境准备](#1. Nginx 环境准备)
- [2. Nginx 安装和启动](#2. Nginx 安装和启动)
- [3. Nginx 常用命令](#3. Nginx 常用命令)
- [4. Nginx 使用systemctl启动、停止、重新加载](#4. Nginx 使用systemctl启动、停止、重新加载)
- [5. Nginx 配置文件](#5. Nginx 配置文件)
- [6. Nginx 配置文件结构](#6. Nginx 配置文件结构)
-
1. Nginx 环境准备
虚拟机镜像,基于Centos7,网盘链接: https://pan.baidu.com/s/1NmCR-vdAcZLouRRn9V1yTA 密码: 1b60,虚拟机的用户名/密码:root/123456,使用的环境和应用放置在/home目录下:
目录 | 简介 | 说明 |
---|---|---|
AdminLTE-3.2.0 | 静态web后台管理系统 | 静态web的配置演示 |
apps/ruoyi-admin.jar | 若依后台管理系统,基于springboot | 方向代理演示 |
apache-tomcat-8.5.81apps/ruoyi-admin.war | javaweb服务器 | 动静分离演示 |
sql/ry_20210924.sqlsql/quartz.sql | 数据库初始化脚本 | 创建若依后台数据库 |
mysql5.7 | 数据库 | 用户名/密码:root/123456 |
open-JDK 1.8 | java运行环境 | 已安装 |
2. Nginx 安装和启动
① 关闭防火墙:
shell
sudo systemctl stop firewalld
sudo systemctl disable firewalld
② 使用yum安装:
shell
sudo yum install yum-utils net-tools
③ 在 CentOS 系统中创建一个名为 nginx.repo 的文件,并将以下内容写入该文件:
shell
[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
这个文件是一个 YUM 软件包管理器的仓库配置文件,它指定了一个名为 nginx-stable 的仓库,其中包含了稳定版本的 Nginx 软件包。这个仓库的 URL 是 http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/,其中 $releasever 和 $basearch 是 CentOS 系统的变量,会被替换为相应的值。此外,这个仓库还启用了 GPG 检查,并指定了 GPG 密钥的 URL。最后,module_hotfixes 参数指定了是否启用模块热修复功能。
执行如下命令:
shell
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
module_hotfixes=true
EOF
shell
sudo yum install nginx
3. Nginx 常用命令
nginx
nginx
#立即停止
nginx -s stop
#执行完当前请求再停止
nginx -s quit
#重新加载配置文件,相当于restart
nginx -s reload
#将日志写入一个新的文件
nginx -s reopen
#测试配置文件
nginx -t
Nginx 日志位于/var/log/nginx/
4. Nginx 使用systemctl启动、停止、重新加载
nginx
systemctl start nginx
systemctl status nginx
#产看日志
journalctl -xe
systemctl stop nginx
systemctl reload nginx
#配置开机启动
systemctl enable nginx
① 在centos 7中,用systemctl启动nginx可能出现如下错误:
shell
[root@nginx-dev ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@nginx-dev ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2023-08-06 17:51:44 CST; 16s ago
Docs: http://nginx.org/en/docs/
Process: 2365 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Aug 06 17:51:44 nginx-dev systemd[1]: Starting nginx - high performance web server...
Aug 06 17:51:44 nginx-dev nginx[2365]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (13: Permission denied)
Aug 06 17:51:44 nginx-dev systemd[1]: nginx.service: control process exited, code=exited status=1
Aug 06 17:51:44 nginx-dev systemd[1]: Failed to start nginx - high performance web server.
Aug 06 17:51:44 nginx-dev systemd[1]: Unit nginx.service entered failed state.
Aug 06 17:51:44 nginx-dev systemd[1]: nginx.service failed.
这是由于selinux的安全策略引起的。解决方法如下:
- setenforce 0 (临时)
- 修改/etc/selinux/config,设置SELINUX=disabled (永久有效,需重启)
② 启动 nginx 报错:
shell
[root@nginx-dev ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@nginx-dev ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2023-08-06 17:56:39 CST; 26s ago
Docs: http://nginx.org/en/docs/
Process: 2403 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Aug 06 17:56:38 nginx-dev nginx[2403]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Aug 06 17:56:38 nginx-dev nginx[2403]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Aug 06 17:56:39 nginx-dev nginx[2403]: nginx: [emerg] still could not bind()
Aug 06 17:56:39 nginx-dev systemd[1]: nginx.service: control process exited, code=exited status=1
Aug 06 17:56:39 nginx-dev systemd[1]: Failed to start nginx - high performance web server.
Aug 06 17:56:39 nginx-dev systemd[1]: Unit nginx.service entered failed state.
Aug 06 17:56:39 nginx-dev systemd[1]: nginx.service failed.
这是一个nginx启动错误,错误信息显示端口80已经被占用。这通常是因为另一个进程已经在监听该端口。您可以使用以下命令检查哪个进程正在使用该端口:
shell
[root@nginx-dev ~]# sudo lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 2355 root 7u IPv4 21150 0t0 TCP *:http (LISTEN)
nginx 2356 nginx 7u IPv4 21150 0t0 TCP *:http (LISTEN)
nginx 2357 nginx 7u IPv4 21150 0t0 TCP *:http (LISTEN)
这将显示所有正在使用端口80的进程的详细信息,包括进程ID(PID)和进程名称。您可以使用此信息来杀死该进程,例如:可以通过杀死该进程:
shell
[root@nginx-dev ~]# sudo kill -9 2355
[root@nginx-dev ~]# sudo kill -9 2356
[root@nginx-dev ~]# sudo kill -9 2357
另一种解决此问题的方法是更改nginx配置文件中的端口。可以编辑nginx配置文件(通常位于/etc/nginx/nginx.conf)并将端口更改为未被占用的端口。然后,重新启动nginx服务以使更改生效:
shell
[root@nginx-dev ~]# sudo service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[root@nginx-dev ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2023-08-06 18:07:52 CST; 23s ago
Docs: http://nginx.org/en/docs/
Process: 2482 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2483 (nginx)
CGroup: /system.slice/nginx.service
├─2483 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─2484 nginx: worker process
└─2485 nginx: worker process
Aug 06 18:07:52 nginx-dev systemd[1]: Starting nginx - high performance web server...
Aug 06 18:07:52 nginx-dev systemd[1]: Started nginx - high performance web server.
5. Nginx 配置文件
nginx 配置文件位于 /etc/nginx/nginx.conf :
nginx
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/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;
}
include 命令会引用 /etc/nginx/conf.d 目录下所有的.conf文件,这样可以保持主配置文件的简洁,同时配个多个.conf文件方便区分,增加可读性。
shell
[root@nginx-dev conf.d]# cd /etc/nginx/conf.d
[root@nginx-dev conf.d]# ls
admin-8000.conf default.conf
默认配置 /etc/nginx/conf.d/default.conf:
nginx
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;
}
}
① 该配置文件定义了一个监听本地 80 端口的服务器,服务器名为 localhost。location /
表示匹配所有以 / 开头的请求 URL,也就是所有请求。
② index 指定了默认的首页文件,当客户端请求一个目录时,Nginx 会尝试返回该目录下的默认文档,则会返回目录列表或者 403 Forbidden 错误。
index 指令的语法如下:
nginx
index file1 [file2 ...];
其中,file1、file2 等参数表示默认文档的文件名,多个文件名之间用空格分隔。Nginx 会按照指定的顺序依次尝试返回这些文件,直到找到一个存在的文件为止。
例如,以下配置指定了默认文档为 index.html:
nginx
location / {
index index.html;
}
当客户端请求 / 目录时,Nginx 会尝试返回 index.html 文件。如果该文件不存在,则会返回目录列表或者 403 Forbidden 错误。
③ root 指令用于指定服务器上的根目录,即 Nginx 服务器将从该目录中提供文件:
shell
[root@nginx-dev conf.d]# cd /usr/share/nginx/html
[root@nginx-dev html]# ll
total 8
-rw-r--r--. 1 root root 497 Apr 12 01:22 50x.html
-rw-r--r--. 1 root root 615 Apr 12 01:22 index.html
该服务器的根目录为 /usr/share/nginx/html,当访问该服务器时,如果请求的路径为 /,则返回 index.html 或 index.htm 文件。如果服务器出现 500、502、503 或 504 错误,则会返回 /usr/share/nginx/html/50x.html 页面。
④ 访问本机的80端口,将返回index.html文件:
shell
[root@nginx-dev ~]# curl 127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nginx-dev ~]#
6. Nginx 配置文件结构
nginx
http {
server{#虚拟主机
location {
listen 80;
server_name localhost;
}
location {
}
}
server{
}
}