安装nginx的一些注意事项

1、如何彻底屏蔽掉Nginx的banner

为了安全或者某些个人的原因,如果要屏蔽掉nginx的banner,要修改以下几个位置:

src/http/ngx_http_header_filter_module.c
src/http/ngx_http_special_response.c
src/core/nginx.h

按照网上绝大部分文章的说法是仅仅修改nginx.h,那么只在一种情况下这种修改是有效的:把server_tokens设置为on;但是,此时在404/403等各种内置错误页面面内看到的底部banner提示也仍然是nginx,所以,必须修改上述的三个文件才能全部屏蔽掉

2、如何修改Nginx的默认错误页面

我这里不是指通过修改nginx.conf或者虚拟主机设置来“指定”错误页面,而是要彻彻底底的修改掉Nginx默认编译进去的404,方法很简单,修改 src/http/ngx_http_special_response.c,在这个文件靠近底部的地方有3xx到5xx各种错误页面的代码

3、如何关闭错误日志

通过error-log off;这种写法是关闭不掉的,如果是通过rpm安装的话,会在/usr/share/nginx下产生一个文件,文件名就是off,错误日志都会写到这里。如果是编译安装的话根据编译路径也一定会生成这个文件。正确的关闭方法是:

error_log /dev/null crit;

加上crit是指只记录级别为严重的错误,日志记录到/dev/null,即空

如果想要默认关闭所有站点的日志,那么可以修改nginx.conf,在http{}里加上:

access_log off;
error_log /dev/null crit;

如果需要给个别主机开启日志,那么在该主机的server{}段内单独指定access_log和error_log即可。

4、Nginx的日志是如何分级的

#define NGX_LOG_STDERR 0
#define NGX_LOG_EMERG 1
#define NGX_LOG_ALERT 2
#define NGX_LOG_CRIT 3
#define NGX_LOG_ERR 4
#define NGX_LOG_WARN 5
#define NGX_LOG_NOTICE 6
#define NGX_LOG_INFO 7
#define NGX_LOG_DEBUG 8

默认的错误等级则是:NGX_LOG_ERR

所以如果你需要详细的调试信息,你应该调整到DEBUG,记录会非常的详尽

5、如何开启Nginx的SSI

添加如下三行

ssi on;
ssi_silent_errors on;
ssi_types text/shtml;

6、如何阻止未经许可的域名指向

修改nginx.conf,添加

server{
listen 80 default;
return 404;
}

7、如何实现Nginx解析静态,而Apache解析动态

一个简单的例子:

server {
listen 80;
server_name proxy.crsay.com;
 
#指定根目录和默认索引文件
location / {
root /www/htdocs/proxysite;
index index.php index.html;
 
# Nginx找不到文件时,转发请求给后端Apache
error_page 404 @proxy;
 
#js/css与图片不记录日志,设置过期时间为1天
location ~ .*\.(js|css)$ {
access_log off;
expires 1d;
}
 
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 1d;
}
}
 
#proxy_pass指定要处理php的服务器地址,如果用ip可以直接写,如果用域名,那么要修改/etc/hosts做本地指向
#动态文件.php请求转发给后端Apache
location ~ \.php$ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://apachesite:81;
}
 
location @proxy {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://apachesite:81;
}
 
 
#指定access日志,关闭error日志
access_log /var/log/nginx/proxysite.crsay.com-access.log;
error_log /dev/null crit;
}

8、nginx.conf设置的一些建议:

1)worker_rlimit_nofile和worker_connections的设置:

这个建议设置为系统ulimit -n得到的数字保持一致

2)gzip

压缩等级建议设置为2

3)log格式设置为如下,保证可以用作awstats分析

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

9,常用的 Nginx 参数和控制

程序运行参数

Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:

-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。

-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

-v:显示 nginx 版本号。

-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。

例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令

sbin/nginx – t – c conf/nginx2.conf

附录:

让Nginx支持Perl

1,安装perl的FCGI模块

perl -MCPAN -e shell
cpan[1]>install FCGI
cpan[1]>install FCGI::ProcManager
cpan[1]>exit

或者直接:

  1. perl -MCPAN -e 'install FCGI'
  2. perl -MCPAN -e 'install FCGI::ProcManager'

cpan中国镜像有两个,但有时候可能会连接不上,执行以下语句然后再安装

# perl -MCPAN -e shell
cpan> o conf urllist unshift http://www.perl.com/CPAN/
cpan> o conf commit

2.获取fastcgi-wrapper.pl

wget http://www.ruby-forum.com/attachment/1583/fastcgi-wrapper.pl

3.开机启动

把文件保存在/usr/local/bin/里,编辑/etc/rc.local让其开机以daemon方式启动:

/usr/local/bin/fastcgi-wrapper.pl &

它会建立/var/run/nginx/perl_cgi-dispatch.sock这个sock,在Nginx的配置文件上要用到。

(确保运行nginx的用户对/var/run/nginx有读写的权限)

查看进程是否启动

ps aux|grep fcgi-wrapper.pl

4.建立perl.conf

location ~ .*\.(pl|cgi)?$$ {
gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME /var/www/awstats/wwwroot/cgi-bin$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}

5、修改Nginx配置文件

在server内添加

include perl.conf;

配置完成,重新启动nginx

nginx -s reload

6、测试 test.cgi

  1. #!/usr/bin/perl
  2. print "Content-type: text/html\n\n";
  3. print "<html><body>Hello, world.</body></html>";

赋予执行权限

chmod +x test.cgi

7、一个简单的控制perl-fcgi的启动脚本

 1 #!/bin/bash
 2 echo
 3 case $1 in
 4  
 5 stop)
 6 /bin/rm -f /var/run/nginx/perl_cgi-dispatch.sock
 7 echo "FastCGI Perl Stopped."
 8 ;;
 9  
10 start)
11 /usr/local/bin/fastcgi-wrapper.pl &
12 echo "FastCGI Perl Started."
13 ;;
14  
15 status)
16 ps aux | grep fastcgi-wrapper
17 ;;
18  
19 restart)
20 /bin/rm -f /var/run/nginx/perl_cgi-dispatch.sock
21 echo "FastCGI Perl Stopped."
22 /usr/local/bin/fastcgi-wrapper.pl &
23 echo "FastCGI Perl Started."
24 ;;
25  
26 *)
27 echo "Usage: ./perl.sh start(stop|restart)"
28 ;;
29  
30 esac
31 echo