Ubuntu 20.04
➜ ~ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal
sudo apt-get install -y nginx
➜ ~ nginx -v
nginx version: nginx/1.17.10 (Ubuntu)
sudo nginx
注:如果Nginx已经在运行,则该命令会报错。
➜ ~ ps -ef | grep -i nginx | grep -v grep
root 3531 1526 0 21:44 ? 00:00:00 nginx: master process nginx
www-data 5558 3531 0 22:10 ? 00:00:00 nginx: worker process
www-data 5559 3531 0 22:10 ? 00:00:00 nginx: worker process
www-data 5560 3531 0 22:10 ? 00:00:00 nginx: worker process
www-data 5562 3531 0 22:10 ? 00:00:00 nginx: worker process
可见有1个master进程和N个(本例中是4个)worker进程。
Nginx启动后,默认使用80端口。
➜ ~ sudo lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3531 root 7u IPv4 74711 0t0 TCP *:http (LISTEN)
nginx 3531 root 8u IPv6 74712 0t0 TCP *:http (LISTEN)
nginx 5558 www-data 7u IPv4 74711 0t0 TCP *:http (LISTEN)
nginx 5558 www-data 8u IPv6 74712 0t0 TCP *:http (LISTEN)
nginx 5559 www-data 7u IPv4 74711 0t0 TCP *:http (LISTEN)
nginx 5559 www-data 8u IPv6 74712 0t0 TCP *:http (LISTEN)
nginx 5560 www-data 7u IPv4 74711 0t0 TCP *:http (LISTEN)
nginx 5560 www-data 8u IPv6 74712 0t0 TCP *:http (LISTEN)
nginx 5562 www-data 7u IPv4 74711 0t0 TCP *:http (LISTEN)
nginx 5562 www-data 8u IPv6 74712 0t0 TCP *:http (LISTEN)
nginx -s quit
sudo nginx -s quit
如果成功,则返回消息为空。
注:如果Nginx并没有在运行,则该命令会报错:
➜ ~ sudo nginx -s quit
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
nginx -t
:➜ ~ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果一切OK,则显示如上信息。否则将会定位到具体哪一行有错误。
注:每次修改完Nginx配置,建议先运行 nginx -t
测试一下。
nginx -s reload
sudo nginx -s reload
如果成功,则返回消息为空。
注:如果Nginx并没有在运行,则该命令会报错:
➜ ~ sudo nginx -s reload
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
注: nginx -s <signal>
是给Nginx发送signal信号,比如 stop
, quit
, reload
, reopen
。其中 stop
会立即停止Nginx,而 quit
和 reload
操作则没那么“暴力”,而是会妥善处理当前正在运行的请求。
Nginx的配置文件是 /etc/nginx/nginx.conf
,打开该文件,里面有
......
http {
......
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
......
server {
listen 6688;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
这段代码表明在6688端口开启HTTP服务。
listen
指定了监听端口location
后面的 /
指明匹配所有requestroot
指定了静态文件的根目录index
指明了默认文件。当request里面没有指定文件名时,会尝试使用默认文件➜ ~ ll /usr/share/nginx/html
total 4.0K
-rw-r--r-- 1 root root 612 Apr 14 22:19 index.html
注:在安装好Nginx,没有任何自定义配置的时候,访问 localhost:80
,得到的其实也是一个类似的html文件,这是Nginx default配置的HTTP service,详见下面。
上面有提到,在 nginx.conf
中:
......
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
......
➜ ~ ll /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 ding ding 34 Jun 29 23:01 default -> /etc/nginx/sites-available/default
在 /etc/nginx/sites-available/default
文件中包含了缺省设置:
......
server {
listen 80 default_server;
......
root /var/www/html;
......
index index.html index.htm index.nginx-debian.html;
......
➜ ~ ll /var/www/html
total 4.0K
-rw-r--r-- 1 root root 612 Jun 29 23:01 index.nginx-debian.html
可见,直接访问80端口时,得到的就是这个 index.nginx-debian.html
文件。
注意, index.html
是排在最前面的,我们可以创建该文件,内容随便写,随后再访问80端口(不需重启Nginx),得到就是 index.html
。
➜ ~ curl localhost
haha, hello!
因篇幅问题不能全部显示,请点此查看更多更全内容