声明:使用的CentOS7操作系统

Nginx命令

执行nginx命令,需要在命令需要使用nginx/sbin目录下的naginx文件。可以是相对路径或者绝对路径

首先进入到nginx/sbin目录下

1
cd /usr/local/nginx/sbin

配置Nginx环境变量,就可以在任意目录使用nginx命令了。

1
vim /etc/profile

在PATH后追加/usr/local/nginx/sbin:,然后保存退出就好了。

然后source /etc/profile,然他立即生效。

查看版本命令:

1
2
3
4
nginx -v

[root@bogon sbin]# nginx -v
nginx version: nginx/1.18.0

检查配置文件的正确性

在启动Nginx服务之前,可以先检查一下conf/nginx.conf配置文件是否有错误。

1
2
3
4
5
nginx -t   #t 是 test

[root@bogon sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动和停止

1
2
3
niginx					#启动nginx服务
ngi #停止nginx服务
ps -ef|grep nginx #启动完成后查看Nginx进程

查看nginx进程,可以的看到有两个进程,第三个是我们执行这条命令对应的进程,可以忽略。主要看前面两条

启动完成会有两个进程,一个work进程和一个master进程,可以通过**修改配置文件[conf/nginx.conf]**,添加更多work进程。

1
2
3
4
[root@bogon sbin]# ps -ef|grep nginx
root 96952 1 0 16:25 ? 00:00:00 nginx: master process ./nginx
nobody 96953 96952 0 16:25 ? 00:00:00 nginx: worker process
root 97742 88062 0 16:25 pts/0 00:00:00 grep --color=auto nginx

nginx默认的端口号是80端口。查看本机的ip地址,然后在windows浏览器的地址栏中,就可以访问nginx静态资源

【记得关linux的防火墙哦….,systemctl stop firewalld

就可以看到以下界面

image-20220804163517886

而且,启动之后,进入nginx根目录,可以看到很多临时目录,以_temp结尾的目录都是临时目录。

1
2
3
4
5
6
7
8
9
10
11
[root@bogon nginx]# ll
total 4
drwx------. 2 nobody root 6 Aug 4 16:20 client_body_temp
drwxr-xr-x. 2 root root 4096 Aug 3 21:47 conf
drwx------. 2 nobody root 6 Aug 4 16:20 fastcgi_temp
drwxr-xr-x. 2 root root 40 Aug 3 21:47 html
drwxr-xr-x. 2 root root 58 Aug 4 16:20 logs
drwx------. 2 nobody root 6 Aug 4 16:20 proxy_temp
drwxr-xr-x. 2 root root 19 Aug 3 21:47 sbin
drwx------. 2 nobody root 6 Aug 4 16:20 scgi_temp
drwx------. 2 nobody root 6 Aug 4 16:20 uwsgi_temp

进入logs目录,就可以看到一些日志文件了

重新加载配置文件

当修改Nginx配置文件后,需要重新加载才能生效,可以使用下面命令重写加载配置文件。

1
nginx -s reload

在启动Nginx后,修改配置文件后,就可以直接使用上面的命令直接生效,不用重启Nginx

Nginx配置文件整体结构

Nginx配置文件(conf/nginx.conf)整体分为三部分:

  • 全局块Nginx运行相关的全局配置
  • events块 和网络连接相关的配置
  • http块 代理、缓存、日志记录、虚拟主机配置
    • http全局块
    • Server块
      • Server全局块
      • location块

http全局块
Server块
Server全局块location块

注意: http块中可以配置多个Server块,每个Server块中可以配置多个location块。

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

#==================全局配置块==================
worker_processes 1;
#==================全局配置块==================


#==================events配置块==================
events {
worker_connections 1024;
}
#==================events配置块==================

#==============================http配置块==============================
http {
#==================http全局配置块==================
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;
#==================http全局配置块==================


#==================Server配置块==================【可以有多个】
server {
listen 80;
server_name localhost;
#=============location配置块============【可以有多个】
location / {
root html;
index index.html index.htm;
}
#=============location配置块============


error_page 500 502 503 504 /50x.html;
#=============location配置块============【可以有多个】
location = /50x.html {
root html;
}
#=============location配置块============
}
#==================Server配置块==================


}
#==============================http配置块==============================

__END__