陈拓 2020.09.17/2023.11.17
1. 概述
百度百科对Nginx 的介绍:
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
上面的介绍知道了Nginx的强大,下面我们实战一下怎样让Nginx支持CGI。
在《树莓派安装Web服务器Boa和CGIC》树莓派安装Web服务器Boa和CGIC_树莓派boa 安装哪一个容器-优快云博客
一文中我们已经在树莓派上安装了一个Web服务器,并且支持CGI,如果需要更好的性能,我们可以在树莓派上安装Nginx作为Web服务器,并让它支持CGI。
2. 安装Nginx
2.1 树莓派换源
见《树莓派安装Web服务器Boa和CGIC》树莓派安装Web服务器Boa和CGIC_树莓派boa 安装哪一个容器-优快云博客
2.2 安装
- 更新软件源
sudo apt-get update
- 安装nginx
sudo apt-get install nginx
- 删除不需要的包
sudo apt autoremove
- 默认目录结构
sudo find / -name nginx
2.3 启动Nginx服务
- 杀掉占用端口80的进程
之前我们安装过Web服务器Boa,占用了端口80,会和Nginx冲突,所以在启动Nginx之前要先杀掉Boa进程。
查Boa进程:
ps -e | grep boa
杀进程463
sudo kill -9 463
为了以后不与Nginx端口冲突,将Boa的端口号改为8080:
sudo nano /etc/boa/boa.conf
- 启动Nginx
sudo /etc/init.d/nginx start
- 查看Nginx进程
ps -e | grep nginx
更详细一些:ps -ef | grep nginx
master是主进程,worker是工作进程。运行主进程的用户是root,运行worker的用户是www-data。这些都在配置文件nginx.conf中描述。我们来看看相关部分。
查找nginx.conf的位置:
sudo find / -name nginx.conf
cat /etc/nginx/nginx.conf
当worker_ processes的值为auto时,nginx会自动检测当前主机的cpu核心数,并启动相应数量的worker进程。我用的树莓派3B+是4核的,所以nginx启动了4个worker进程。每个worker进程的最大连接数由worker_connections指定,这里是768。
2.4 测试
在浏览器地址栏输入http://raspberrypi.local/
2.5 配置文件
安装完成后nginx有2个配置文件::
/etc/nginx/sites-available/default
/etc/nginx/nginx.conf
- 默认配置文件/etc/nginx/sites-available/default
Nginx的默认配置文件去掉注释就只有下面几行:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
通常不修改默认配置文件。
- 主配置文件/etc/nginx/nginx.conf
可以通过修改主配置文件/etc/nginx/nginx.conf对Nginx进行配置。在配置比较多时使用子配置文件比较方便。
- 子配置文件/etc/nginx/conf.d/*.conf
子配置文件目录/etc/nginx/conf.d/在Nginx刚安装完时是空的,我们可以在 conf.d目录下创建多个扩展名为.conf的子配置文件。这样做的好处是在conf.d目录中增加或者删除子配置文件时,只需要重新加载Nginx即可,不需要修改nginx.conf文件,以方便管理。
在主配置文件nginx.conf中使用include关键字:
include /etc/nginx/conf.d/*.conf;
将目录conf.d中所有的.conf文件都包含进来。
- 在主配置文件中包含默认配置文件
在主配置文件nginx.conf有:
include /etc/nginx/sites-enabled/*;
而/etc/nginx/sites-enabled/default链接到了默认配置文件/etc/nginx/sites-available/default:
这样就将默认配置文件包含到了主配置文件中。
3. 让Nginx支持CGI
Nginx不能直接执行CGI程序,CGI程序可以通过FastCGI接口来调用。为了调用CGI程序,我们需要一个FastCGI的包装器wrapper,这个wrapper绑定在某个端口上。当nginx将CGI请求发送给这个端口时,wrapper接纳请求并fork一个新的线程,这个线程调用CGI程序并读取返回值,wrapper再将返回的数据通过绑定的端口传递给 Nginx。
3.1 安装spawn-fcgi
spawn-fcgi是通用FastCGI进程管理器,用来运行fcgiwrap。
sudo apt-get install spawn-fcgi
3.2 安装fcgiwrap
Github网址:https://github.com/gnosek/fcgiwrap
Fcgiwrap是CGI脚本的FastCGI包装器。
sudo apt-get install fcgiwrap
fcgiwrap的安装位置:sudo find / -name fcgiwrap
3.3启动spawn-fcgi管理fcgiwrap进程
- 绑定服务IP和端口,指定fcgiwrap目录
spawn-fcgi -a 127.0.0.1 -p 8888 -f /usr/sbin/fcgiwrap
- 查看8888端口
netstat -na | grep 8888
sudo netstat -tlnp | grep fcgiwrap
- 查看进程
ps -ef | grep fcgi
3.4 WWW根目录和CGI目录
在默认配置文件/etc/nginx/sites-available/default中我们看到Nginx的www默认根目录root为/var/www/html,测试页面index.nginx-debian.html就放在这里。
也可以使用目录/usr/share/nginx/html作为root目录,其中也有一个和index.nginx-debian.html内容同样的index.html页面。
之前在《树莓派安装Web服务器Boa和CGIC》
一文中我们已经在目录/var/www下创建了我们自己的默认页面文件index.html。
在/var/www/cgi-bin目录下创建了一个测试cgi文件test.cgi,我们就用它们进行测试。
3.5 修改nginx.conf设置WWW根目录
更改nginx.conf配置文件,让nginx转发请求。
我们对nginx的配置写在nginx.conf文件里,nginx.conf在目录/etc/nginx中,这个目录下还有conf.d和sites-enabled两个文件夹,里面为默认的配置文件。在编辑nginx.conf文件时,需要把相关行注释掉,否则nginx.conf不会生效。
- 先备份一下nginx.conf
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf-bak
- 修改nginx.conf
sudo nano /etc/nginx/nginx.conf
先找到下面2行注释掉:
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
找到:
default_type application/octet-stream;
在下面添加:
server {
listen 80;
server_name localhost;
root /var/www/;
location / {
try_files $uri $uri/ /index.html;
}
}
- 检查配置文件是否正确
sudo nginx -t
3.6 重启nginx或者重新加载配置文件
- 重新启动
sudo killall nginx
sudo /etc/init.d/nginx start
- 重新加载
sudo /etc/init.d/nginx reload
3.7 测试
在浏览器地址栏输入http://raspberrypi.local/
我们在《树莓派安装Web服务器Boa和CGIC》中写的index.html页面出来了。
3.8 修改nginx.conf设置CGI
修改server块,添加中间的location部分:
server {
listen 80;
server_name localhost;
root /var/www/;
location ~ \.cgi$ {
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.cgi;
include fastcgi.conf;
}
location / {
try_files $uri $uri/ /index.html;
}
}
3.9 测试
在浏览器地址栏输入http://raspberrypi.local/cgi-bin/test.cgi
用IP地址测试:
和《树莓派安装Web服务器Boa和CGIC》一文的结果一样,但这是由Nginx运行的test.cgi。
3.10 完整的配置文件nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
root /var/www/;
location ~ \.cgi$ {
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.cgi;
include fastcgi.conf;
}
location / {
try_files $uri $uri/ /index.html;
}
}
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
3.11 开机启动fcgiwrap
- 修改rc.local文件
sudo nano /etc/rc.local
在打开的文本中找到exit 0,在此之前添加的代码在启动时都会被执行,在exit 0 之前添加一行代码:
su pi -c "spawn-fcgi -a 127.0.0.1 -p 8888 -f /usr/sbin/fcgiwrap"
su命令是指定在pi用户下执行这条命令,-c 表示执行完这条命令之后恢复原来的用户。
注意:系统启动时在执行这段代码时是使用root用户权限的,如果不指定pi用户,可能会因为权限问题导致脚本执行失败。
- 重新启动系统测试
sudo reboot
- 进入终端查看已经启动的fcgiwrap进程
ps -e | grep fcgiwrap
参考文档
- 树莓派安装Nginx
https://www.cnblogs.com/xiangzhuo/p/9473812.html - nginx配置cgi
nginx配置cgi_cgi nginx-优快云博客 - nginx配置不生效,页面一直是默认页面welcome to nginx的解决办法
nginx配置不生效,页面一直是默认页面welcome to nginx的解决办法-优快云博客