nginx 和 cgi, fcgi 以及php-cgi, php-fpm

在apache 称雄的时代,经常会看到cgi,但是随着nginx 的出现以及时代的进步, cgi 的程序越来越少了.

最近有需求安装smokeping,因此cgi 又被拿了出来

CGI是common gateway interface的缩写,大家都译作通用网关接口,但很不幸,我们无法见名知意。

我们知道,web服务器所处理的内容都是静态的,要想处理动态内容,需要依赖于web应用程序,如php、jsp、python、perl等。但是web server如何将动态的请求传递给这些应用程序?它所依赖的就是cgi协议。没错,是协议,也就是web server和web应用程序交流时的规范。换句话说,通过cgi协议,再结合已搭建好的web应用程序,就可以让web server也能”处理”动态请求(或者说,当用户访问某个特定资源时,可以触发执行某个web应用程序来实现特定功能),你肯定知道处理两字为什么要加上双引号。CGI可以是任何的可执行程序,可以是Shell脚本,二进制应用,或者其他的脚本(Python脚本,Ruby脚本等)

简单的cgi工作方式如下:

有多种方式可以执行cgi程序,但对http的请求方法来说,只有get和post两种方法允许执行cgi脚本。实际上post方法的内部本质还是get方法,只不过在发送http请求时,get和post方法对url中的参数处理方式不一样而已。

任何一种语言都能编写CGI,只不过有些语言比较擅长,有些语言则非常繁琐,例如用bash shell开发,那么需要用echo等打印语句将执行结果放在巨多无比的html的标签中输出给客户端。常用于编写CGI的语言有perl、php、python等,java也一样能写,但java的servlet完全能实现CGI的功能,且更优化、更利于开发

总体上来说,CGI(common gateway interface) 就是所谓的短生存应用程序,Fast CGI 就是所谓的长生存应用程序. FastCGI 像是一个常驻 long-live 型的CGI, 它可以一直执行者,不会每次都要话费时间去fork一次

CGI 和 fastcgi 有自己输入和输出标准,比如HTTP头部, CGI环境变量,get和post等等

CGI, Fast-CGI 是protocols, CGI 慢点, Fast-CGI 要快很多

CGI程序能够用 Python, PERL, Shell, C or C++等语言来实现,尽管没有明确的规定,但是一般用C写的cgi,我们会用.cgi作为后缀,用perl 的用.pl作为后缀,其实我们都可以用.cgi作为后缀. Perl由于其跨操作系统、易于修改的特性成为了CGI的主流编写语言,以至于一般的“cgi程序”就是Perl程序

nginx + fastcgi: nginx只能处理静态文件,对于动态文件,一般用fastcgi 来作为“沟通”的协议. fastcgi 进程由fastcgi 进程管理器管理,而不是nginx,这样就需要一个fastcgi 管理器,这里可以使用spawn-fcgi 作为fastcgi 的进程管理器

nginx + cgi: nginx 不能直接执行外部可执行程序,因此nginx天生不支持cgi的, nginx 虽然不支持cgi,但是他“支持”fastCGI, 这样我们可以fastcgi 来wrap 一下cgi,这样变相的支持cgi,常见的fastcgi wrapper 有fcgiwrap

另外其实cgi 程序也可以被当成fastCGI,因此也可以用nginx + spawn-fcgi 来执行cgi

Spawn-FCGI是一个通用的FastCGI管理服务器,它是lighttpd中的一部份,很多人都用Lighttpd的Spawn-FCGI进行FastCGI模式下的管理工作,不过有不少缺点。而PHP-FPM的出现多少缓解了一些问题

fastcgi 是一个协议, php-fpm 实现了这个协议

通过webserver 来运行php, 一般有两种方式,一个是php-cgi, 另外一个php-fpm (php FastCGI Process Manager) (apache 的mod_php, 把php当作一个模块来执行,以及cli 和 isapi 不在考虑范围之内, php总共来说有5种执行模式)

php-fpm 比传统的CGI方式 (suPHP) 要快,

php-cgi是php自带的fastcgi进程管理器,php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启。另外直接杀死php-cgi进程,php就不能运行了,但是php-fpm和 spawn-fcgi就没有此类问题

php-fpm 就是一个支持php 解析的 fastcgi进程管理器,只能适用于php,其余语言写的cgi,例如perl,python,C,都不能使用

mod_php 和 php-fpm 是运行php 的两种方式,mod_php 是running php as apache module

在segmentfault上看到了一个文章,写的很不错https://segmentfault.com/q/1010000000256516

你(PHP)去和爱斯基摩人(web服务器,如 Apache、Nginx)谈生意

你说中文(PHP代码),他说爱斯基摩语(C代码),互相听不懂,怎么办?那就都把各自说的话转换成英语(FastCGI 协议)吧。

怎么转换呢?你就要使用一个翻译机(PHP-FPM)
(当然对方也有一个翻译机,那个是他自带的)

我们这个翻译机是最新型的,老式的那个(PHP-CGI)被淘汰了。不过它(PHP-FPM)只有年轻人(Linux系统)会用,老头子们(Windows系统)不会摆弄它,只好继续用老式的那个。

Smokeping 常用操作

在centos上安装了smokeping 以后,我们可能会经常遇到各种问题

  1. 对target进行修改,修改完以后,我们需要重启smokeping service ,一般情况下是不需要重启httpd,因为smokeping 的主程序会查看config 的timestamp,如果有变化就会重新读取config 配置文件,但是如果你把target 通过include 的方式包含在config当中,那么即使你修改了target文件,但是config 文件的timestamp 仍然不变。经过测试,解决办法有两个: 要么稍微修改一下config 文件,添加或者删除一些无关紧要的文字,要不然就在重启完smokeping以后再重启一下httpd服务
  2. 还有一个经常见的问题就是slave 的图没有数据,这个一般是权限的问题。因为slave 端的数据,是slave 端通过http call,写入master 端的,而master 端完成此项任务的是httpd,因此httpd 要对 data 文件夹下面的target 的文件夹要有写入权限。实现这个目标,可以有多种实现的办法,一种办法是利用getfacl 和 setfacl 来设置apache 对这些目录单独的权限,另外一种办法是把这些文件的group 设置为apache,并且给apache 写入的权限 chmod g+w *
  3. 查看rrd文件里面的是否用数据,例如可以用rrdtool fetch Akamai1~10.10.16.111.rrd AVERAGE 命令查看master端slave提交的rrd文件

阿里轻量云香港

阿里轻量云香港测试IP为149.129.84.1,新加坡测试IP: 149.129.49.31

移动联通直连,但是电信去程为NTT,回程为CN2

被用来服务国内的移动和联通客户还是很不错的

阿里轻量云和普通ECS的区别就是没有内网IP(自带的内网IP无法和ECS的内网互联),另外也没有那2G的防御

同样的腾讯云香港的测试IP为119.28.11.12

还有华为云香港的测试IP为159.138.33.92

GCP的CDN 35.244.179.64

K2P刷官改

K2P A2版因为强大的可玩性,遭到了很多玩家的吹捧,而事实上也确实如此,记住K2P 要收的话一定要收A2版

如果侧重于玩,那么openwrt无疑是最好的选择,可是如果仅仅是为了跑稳定的服务,那么K2P官改无疑是最好的选择.

K2P的破解,随着K2P自带系统的不断升级,越来越难,现在的K2P一般自带的是V22.5.7.85

本方法适用于最新的K2P MTK 22.8.5.189、22.10.2.23、22.10.2.24固件。

一般来说,只需使用下面的第一个工具,刷入breed然后下载固件,在breed里刷入固件即可

更多

Bind 的 forwarding 和 recursion

很少接触bind的各种服务,可是几乎每年都会用到一次两次,每次都得重新搜索bind 的文档,太麻烦了。。下面是在stackoverflow上看到关于bind 的forwarding 和 recursion 的描述,写的很好.

Forwarding: just passes the DNS query to another DNS server (e.g. your ISP’s). Home routers use forwarding to pass DNS queries from your home network’s clients to your ISP’s DNS servers. For example, for foo.example.com, a forwarding DNS server would first check its cache (did it already ask this question before), and if the answer is not in its cache, it would ask its forwarder (your ISP’s DNS server) for the answer, which would respond with either a cached response, or would perform recursion until it figured out the answer.

Recursion: or caching server, the DNS server receiving the query takes it upon itself to figure out the answer to that query by recursively querying authoritative DNS servers for that domain. For example, for foo.example.com, a recursor would first query the root servers for what DNS servers are responsible for the .com TLD, then it would ask those servers for example.com, then it would query the servers for example.com for foo.example.com, finally getting the answer to the original query.

In terms of security, you should separate recursors/forwarders (typically DNS servers used to service a bunch of clients) and authoritative DNS servers (typically these are responsible ONLY for answering queries re: domains that they are authoritative for – these servers will NOT perform recursive queries for anyone).

在centos 7上安装bind 并把它设置为resursor server 最简单了,直接 yum install bind bind-utils就可以了,bind 的默认named.conf 就是一个完整只适用于本机localhost 的 resursor server 设置

centos 7 安装geoipupdate

很多人在安装完centos 7 mini iso 以后会安装bind-utils,bind-utils 包含了许多DNS查询的工具包,其中就有一个dependency GeoIP library

但是这个GeoIP library 是包含在老版的geoipupdate这个程序中的,版本号应该为1.5,但是maxmind 早就放弃了对于老版geoipupdate的支持,因此如果你的程序需要geoipupdate 的支持的话,用centos 7 默认repo 自带的geoipupdate 的话,肯定是不行的了

因此我们肯定是要安装经过重新开发过的只支持新的geoip 数据库格式的geoipupdate

github地址是https://github.com/maxmind/geoipupdate

但是这里又出现了一个问题,如果我们直接使用rpm 的安装包来安装的话,默认的可执行command 会被安装在/usr/bin/geoipupdate, conf 地址会被安装在/etc/GeoIP.conf, 这样会和老版的geoipupdate 发生冲突,但是bind-utils 又依赖于老版的GeoIP 库,不可能卸载掉

这里有一个比较简单的解决办法,就是直接安装linux 的通用安装包,而不是用专门的centos的rpm安装包,这样可执行程序的路径就可以被我们自行选择

bootstrap中的sr-only

最近在看bootstrap4,经常会有sr-only这个类

sr是screen reader的缩写,这个表示是为视力有障碍的人士准备的类.

在前端开发中,有些时候设计图上面会出现仅供正常视觉用户看的元素。比如:导航栏菜单当前页面选中高亮状态,这些状态只有视力正常的人才能正常使用。

而残障人士,弱势或盲人是很难或者根本看不出导航菜单高亮的。他们上网可能借助的是屏幕阅读器,也就是 screen reader(sr),屏幕阅读器需要找到能辨识的文本说明然后“读”出来给用户听。

CentOS 7 Nginx 默认配置

在centos 7 下,用nginx official repo安装的nginx stable版本的config 默认配置如下:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
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;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

但是很明显, 这个配置过于简单了,结合军哥的LNMP,我们可以稍微改动一下config:

user nginx;

worker_processes auto;
worker_cpu_affinity auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
multi_accept off;
accept_mutex off;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
sendfile_max_chunk 512k;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;


gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.


server_tokens off;
access_log off;

server {

listen 80 default_server;
server_name _;
index index.html index.htm index.php;
root /usr/share/nginx/html;


}


include /etc/nginx/conf.d/*.conf;
}

在CentOS7上,nginx的启动是有systemd 来管理的,位置在/usr/lib/systemd/system/nginx.service

配置文件为:

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target