码迷,mamicode.com
首页 > 其他好文 > 详细

安装nginx的一些注意事项

时间:2015-10-01 01:40:53      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

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{}里加上:

  1. access_log off;
  2. error_log /dev/null crit;

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

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

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

默认的错误等级则是:NGX_LOG_ERR
所以如果你需要详细的调试信息,你应该调整到DEBUG,记录会非常的详尽

5、如何开启Nginx的SSI

添加如下三行

  1. ssi on;
  2. ssi_silent_errors on;
  3. ssi_types text/shtml;

6、如何阻止未经许可的域名指向
修改nginx.conf,添加

  1. server{
  2. listen 80 default;
  3. return 404;
  4. }

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

一个简单的例子:

  1. server {
  2. listen 80;
  3. server_name proxy.crsay.com;
  4.  
  5. #指定根目录和默认索引文件
  6. location / {
  7. root /www/htdocs/proxysite;
  8. index index.php index.html;
  9.  
  10. # Nginx找不到文件时,转发请求给后端Apache
  11. error_page 404 @proxy;
  12.  
  13. #js/css与图片不记录日志,设置过期时间为1天
  14. location ~ .*\.(js|css)$ {
  15. access_log off;
  16. expires 1d;
  17. }
  18.  
  19. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
  20. access_log off;
  21. expires 1d;
  22. }
  23. }
  24.  
  25. #proxy_pass指定要处理php的服务器地址,如果用ip可以直接写,如果用域名,那么要修改/etc/hosts做本地指向
  26. #动态文件.php请求转发给后端Apache
  27. location ~ \.php$ {
  28. proxy_set_header Host $http_host;
  29. proxy_set_header X-Real-IP $remote_addr;
  30. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  31. proxy_pass http://apachesite:81;
  32. }
  33.  
  34. location @proxy {
  35. proxy_set_header Host $http_host;
  36. proxy_set_header X-Real-IP $remote_addr;
  37. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  38. proxy_pass http://apachesite:81;
  39. }
  40.  
  41.  
  42. #指定access日志,关闭error日志
  43. access_log /var/log/nginx/proxysite.crsay.com-access.log;
  44. error_log /dev/null crit;
  45. }

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"‘;

附录:

让Nginx支持Perl

1,安装perl的FCGI模块

  1. perl -MCPAN -e shell
  2. cpan[1]>install FCGI
  3. cpan[1]>install FCGI::ProcManager
  4. cpan[1]>exit

或者直接:

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

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

  1. # perl -MCPAN -e shell
  2. cpan> o conf urllist unshift http://www.perl.com/CPAN/
  3. 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

  1. location ~ .*\.(pl|cgi)?$$ {
  2. gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
  3. fastcgi_pass unix:/var/run/nginx/perl_cgi-dispatch.sock;
  4. fastcgi_index index.cgi;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. #fastcgi_param SCRIPT_FILENAME /var/www/awstats/wwwroot/cgi-bin$fastcgi_script_name;
  7. fastcgi_param QUERY_STRING $query_string;
  8. fastcgi_param REQUEST_METHOD $request_method;
  9. fastcgi_param CONTENT_TYPE $content_type;
  10. fastcgi_param CONTENT_LENGTH $content_length;
  11. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  12. fastcgi_param SERVER_SOFTWARE nginx;
  13. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  14. fastcgi_param REQUEST_URI $request_uri;
  15. fastcgi_param DOCUMENT_URI $document_uri;
  16. fastcgi_param DOCUMENT_ROOT $document_root;
  17. fastcgi_param SERVER_PROTOCOL $server_protocol;
  18. fastcgi_param REMOTE_ADDR $remote_addr;
  19. fastcgi_param REMOTE_PORT $remote_port;
  20. fastcgi_param SERVER_ADDR $server_addr;
  21. fastcgi_param SERVER_PORT $server_port;
  22. fastcgi_param SERVER_NAME $server_name;
  23. }

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

安装nginx的一些注意事项

标签:

原文地址:http://www.cnblogs.com/mengdeep/p/4850577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!