Ubuntu 20.04
➜ ~ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04 LTS Release: 20.04 Codename: focal注:如果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) 通过HTTP方式访问localhost 停止Nginx: 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)如果一切OK,则显示如上信息。否则将会定位到具体哪一行有错误。
注:每次修改完Nginx配置,建议先运行 nginx -t 测试一下。
重启Nginx: 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/*; } ......在 /etc/nginx/conf.d 目录新建一个后缀名为 conf 的文件,比如 ding.conf ,内容如下:
server { listen 6688; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } }这段代码表明在6688端口开启HTTP服务。
listen 指定了监听端口location 后面的 / 指明匹配所有requestroot 指定了静态文件的根目录index 指明了默认文件。当request里面没有指定文件名时,会尝试使用默认文件比如,现在当我访问6688端口的 / 目录时,实际上就是访问了servers上的 /usr/share/nginx/html/index.html 文件:
注:该html文件是伴随Nginx安装自动生成的:
➜ ~ 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/*; ......其中 /etc/nginx/conf.d 目录默认是空的,而 /etc/nginx/sites-enabled 目录则包含了一个软链接文件 default :
➜ ~ 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; ......其中,设置了根目录为 /var/www/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!