nginx配置-最后整理版

  1. nginx_upstream_check_module
  2. nginx-module-vts
  3. nginx打补丁

nginx编译安装

  1. - 下载
  2. cd /usr/local/src/
  3. wget http://nginx.org/download/nginx-1.12.2.tar.gz
  4. tar -xf nginx-1.12.2.tar.gz
  5. - 准备环境
  6. useradd -s /sbin/nologin -M www
  7. yum install perl-devel openssl-devel gcc -y
  8. - 编译安装
  9. cd /usr/local/src/nginx-1.12.2
  10. ./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
  11. make && make install
  12. ln -s /usr/local/nginx-1.12.2 /usr/local/nginx
  13. cd /usr/local/nginx/conf
  14. egrep -v '^$|#' nginx.conf.default > nginx.conf
  15. echo "PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile
  16. source /etc/profile
  17. - 管理命令
  18. nginx -t #检查语法
  19. nginx -s stop
  20. nginx -s reload

注: Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。

nginx安全优化

  1. 改软件名
  2. 改版本号
  3. 限制访问(dir/file/扩展名)
  4. 限制访问方法
  5. 限制上传大小
  6. 防盗链
  7. 防爬虫
  8. 防非法解析
  9. 目录安全
  10. 使用普通用户运行
  11. www运行worker
  12. 错误页优化

改软件名

  1. - 查看下jd的,竟然是JDWS/2.0
  2. [root@n1 conf]# curl -I www.jd.com
  3. HTTP/1.1 302 Moved Temporarily
  4. Server: JDWS/2.0
  5. - 1.修改内核(头部信息)
  6. [root@n1 nginx-1.12.2]# vim /usr/local/src/nginx-1.12.2/src/core/nginx.h
  7. ...
  8. 13 define NGINX_VERSION "7.0"
  9. 14 define NGINX_VER "MTWS/" NGINX_VERSION
  10. ...
  11. - 2.修改头部
  12. [root@n1 nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c
  13. 49 static u_char ngx_http_server_string[] = "Server: MTWS" CRLF;
  14. - 3.修改错误页(response header)
  15. [root@n1 nginx-1.12.2]# vim src/http/ngx_http_special_response.c
  16. 22 "<hr><center>MTWS</center>" CRLF
  17. ...
  18. 29 "<hr><center>MTWS</center>" CRLF
  19. ...
  20. 36 "<hr><center>MTWS</center>" CRLF
  21. - 修改结果检查
  22. [root@n1 src]# grep -nir mtws .
  23. ./core/nginx.h:14:#define NGINX_VER "MTWS/" NGINX_VERSION
  24. ./core/nginx.h:22:#define NGINX_VAR "MTWS"
  25. ./http/ngx_http_header_filter_module.c:49:static u_char ngx_http_server_string[] = "Server: MTWS" CRLF;
  26. ./http/ngx_http_special_response.c:22:"<hr><center>MTWS</center>" CRLF
  27. ./http/ngx_http_special_response.c:29:"<hr><center>MTWS</center>" CRLF
  28. ./http/ngx_http_special_response.c:36:"<hr><center>MTWS</center>" CRLF
  29. - 然后编译安装(清理下之前的环境),测试
  30. [root@n1 ~]# curl -I 192.168.2.11
  31. HTTP/1.1 200 OK
  32. Server: MTWS/7.0.0
  33. - 重新编译
  34. nginx -s stop
  35. rm -rf /usr/local/nginx*
  36. cd /usr/local/src/nginx-1.12.2
  37. ./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
  38. make && make install
  39. ln -s /usr/local/nginx-1.12.2 /usr/local/nginx

改版本号

  1. - 默认有版本号
  2. [root@n1 ~]# curl -I 192.168.2.11
  3. #获取的是响应头信息
  4. HTTP/1.1 200 OK
  5. Server: **nginx/1.12.2**
  6. ...
  7. - 修改nginx.conf
  8. http{
  9. ...
  10. server_tokens off;
  11. ...
  12. }
  13. - 再次测试,发现没版本号了
  14. [root@n1 ~]# curl -I 192.168.2.11
  15. HTTP/1.1 200 OK
  16. Server: MTWS #这里的版本号没了
  17. ...

限制访问(dir/file/扩展名)

站点目录及文件访问控制

  1. http -> server -> location
  2. location匹配 dir/file/扩展名 来通过ip 阻断
  3. 重定向到相应页面
  4. [location语法](http://www.cnblogs.com/iiiiher/p/8545244.html)

限制访问方法

利用Nginx限制HTTP的请求方法

  1. #Only allow these request methods
  2. if $request_method !~ ^(GET|HEAD|POST$ {
  3. return 501;
  4. }
  5. #Do not accept DELETE,SEARCH and other methods

限制上传大小

上传文件大小的限制(动态应用)

nginx限制客户端上传附件的大小

防盗链(HTTP referer)

盗链和防盗链场景模拟实现 针对方法1做了改善.

  1. 根据HTTP referer实现防盗链, 利用referer,并且针对扩展名rewrite重定向,下面的代码为利用referer且针对扩展名rewrite重定向,即实现防盗链的Nginx配置。
  2. 根据cookie防盗链
  3. 通过加密变换访问路径实现防盗链
  4. 在所有网站资源上添加网站信息,让盗链人员帮你做推广宣传
  • 设置expires的方法如下:
  1. server {
  2. listen 80;
  3. server_name www.maotai.com;
  4. root html/www;
  5. index index.html index.htm;
  6. access_log logs/www_access.log main;
  7. #Preventing hot linking of images and other file types
  8. location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
  9. valid_referers none blocked server_names *.maotai.com maotai.com;
  10. if ($invalid_referer){
  11. rewrite ^/ http://www.maotai.com/img/nolink.jpg;
  12. }
  13. access_log off;
  14. root html/www;
  15. expires 1d;
  16. break;
  17. }
  18. }

防爬虫

范例1:阻止下载协议代理,命令如下:

  1. ## Block download agents ##
  2. if $http_user_agent ~* LWP::Simple|BBBike|wget
  3. {
  4. return 403;
  5. }

范例2:添加内容防止N多爬虫代理访问网站,命令如下:

这些爬虫代理使用“|”分隔,具体要处理的爬虫可以根据需求增加或减少,添加的内容如下:

  1. if $http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo!Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot"
  2. {
  3. return 403;
  4. }

防非法解析

禁止非法域名解析到本网站

  • 第一种方式:配置一个server虚拟主机区块,放置在所有server区块最前面
  1. server {
  2. listen 80;
  3. server_name - ;
  4. return 501;
  5. }

  • 第二种方式:将计就计,通过你的域名访问时候,自动跳转到我的域名上
  1. server {
  2. listen 80 default_server;
  3. server_name _;
  4. rewrite ^(.*) http://www.maotai.com/$1 permanent;
  5. }
  6. if ($host !~ ^www\.maotai\.com$)
  7. {
  8. rewrite ^(.*) http://www.maotai.com/$1 permanent;
  9. }

目录安全

Nginx站点目录文件及目录权限优化

使用普通用户启动nginx

1、切换到普通用户家目录下,创建nginx所需文件

  1. [nginx@web01 ~]$ mkdir -p blog/{conf,logs,html}
  2. [nginx@web01 ~]$ cd blog/
  3. [nginx@web01 blog]$ cp /usr/local/nginx/conf/nginx.conf.default ./conf/
  4. [nginx@web01 blog]$ grep -vE "^$|#" conf/nginx.conf.default > conf/nginx.conf
  5. [nginx@web01 blog]$ cp /usr/local/nginx/conf/mime.types conf/

2、编写配置文件

  1. worker_processes 4;
  2. worker_cpu_affinity 0001 0010 0100 1000;
  3. worker_rlimit_nofile 65535;
  4. error_log /home/nginx/blog/logs/error.log;
  5. user inca inca;
  6. pid /home/nginx/blog/logs/nginx.pid;
  7. events {
  8. use epoll;
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type usr/local/octet-stream;
  14. sendfile on;
  15. keepalive_timeout 65;
  16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. '$status $body_bytes_sent "$http_referer" '
  18. '"$http_user_agent" "$http_x_forwarded_for"';
  19. server {
  20. listen 8080;
  21. server_name www.maotai.com;
  22. root /home/nginx/blog/html;
  23. location / {
  24. index index.html index.htm;
  25. }
  26. access_log /home/nginx/blog/logs/web_blog_access.log main;
  27. }
  28. }

注意:普通用户不能使用知名端口,需要使用其他端口启动服务

3、检查配置文件语法,并启动nginx服务

  1. /usr/local/nginx/sbin/nginx -t -c /home/nginx/blog/conf/nginx.conf

  2. /usr/local/nginx/sbin/nginx -c /home/nginx/blog/conf/nginx.conf &>/dev/null &
  3. 注意:忽略一些不正确的输出信息

worker进程以www用户运行

  1. [root@n1 nginx]# ps -ef|grep nginx
  2. root 12067 1 0 09:01 ? 00:00:00 nginx: master process nginx
  3. **www** 12152 12067 0 09:04 ? 00:00:00 nginx: worker process
  4. - 方法1: 编译时候指定
  5. useradd -s /sbin/nologin -M www
  6. ./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
  7. 方法2: 修改nginx.conf
  8. user www www; # 用户名,用户组

错误页优化

NGINX错误页面友好显示

范例1:对错误代码403实行本地页面跳转,命令如下:

  1. server {
  2. listen 80;
  3. server_name www.maotai.com;
  4. location / {
  5. root html/www;
  6. index index.html index.htm;
  7. }
  8. error_page 403 /403.html; #<==当出现403错误时,会跳转到403.html页面 /403.html是相对于站点根目录html/www的。
  9. }

范例2:50x页面放到本地单独目录下,进行优雅显示。

  1. # redirect server error pages to the static page /50x.html
  2. error_page 500 502 503 504 /50x.html;
  3. location = /50x.html {
  4. root /data0/www/html;
  5. }

范例3:改变状态码为新的状态码,并显示指定的文件内容,命令如下:

  1. error_page 404 =200 /empty.gif;
  2. server {
  3. listen 80;
  4. server_name www.maotai.com;
  5. location / {
  6. root /data0/www/bbs;
  7. index index.html index.htm;
  8. fastcgi_intercept_errors on;
  9. error_page 404 =200 /ta.jpg;
  10. access_log /app/logs/bbs_access.log commonlog;
  11. }
  12. }

范例4:错误状态码URL重定向,命令如下:

  1. server {
  2. listen 80;
  3. server_name www.maotai.com;
  4. location / {
  5. root html/www;
  6. index index.html index.htm;
  7. error_page 404 https://clsn.cnblogs.com;
  8. #<==当出现404错误时,会跳转到指定的URL https://clsn.cnblogs.com页面显示给用户,这个URL一般是企业另外的可用地址
  9. access_log /app/logs/bbs_access.log commonlog;
  10. }
  11. }

[nginx]编译安装及安全优化的更多相关文章

  1. Linux下nginx编译安装教程和编译参数详解

    这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # y ...

  2. nginx编译安装

    Nginx编译安装 1.nginx官网:http://wiki.nginx.org/Install下载:http://nginx.org/en/download.html 2.编译安装# wget h ...

  3. LNMP平台搭建之一:nginx编译安装

    参考博客:https://www.cnblogs.com/zhang-shijie/p/5294162.html   jack.zhang 一.环境说明 系统环境:centos6.5 [root@lo ...

  4. Nginx编译安装lua-nginx-module

    lua-nginx-module 模块可以将Lua的强大功能嵌入NGINX服务器. 下载Nginx源码 如果已安装Nginx,需要查看当前安装版本的编译参数: $ /usr/local/nginx/s ...

  5. Nginx编译安装:

    第三方模块 在nginx.org   --------  wiki  找 --add-module=   添加 Nginx编译安装: 安装开发环境 ]# yum groupinstall " ...

  6. 20190418 CentOS7实用技能综合:系统安装 + WinScp客户端连接 + 防火墙端口号iptables + Nginx编译安装 + MySQL编译安装 + Redis编译安装 + MongoDB编译安装 + ActiveMQ/RocketMQ/RabbitMQ编译安装 + ...各类常用生产环境软件的编译安装

    系统安装 + WinScp客户端连接 + 防火墙端口号iptables + Nginx编译安装 + MySQL编译安装 + Redis编译安装 + MongoDB编译安装 + ActiveMQ/Roc ...

  7. Nginx编译安装第三方模块http_substitutions_filter_module2222

    Nginx编译安装第三方模块http_substitutions_filter_module Rming -- 阅读 安装 Http 编译 module filter nginx 模块 >> ...

  8. Nginx编译安装第三方模块http_substitutions_filter_module

    Nginx编译安装第三方模块http_substitutions_filter_module 分类:服务器技术  作者:rming  时间:-- . >>ngx_http_substitu ...

  9. nginx编译安装新模块

    nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 这里以安装第三方ngx_http_google_filter_module模块为例 下载第三方扩展模块ngx_ht ...

随机推荐

  1. V-rep学习笔记:Reflexxes Motion Library 1

    V-REP中集成了在线运动轨迹生成库Reflexxes Motion Library Type IV,目前Reflexxes公司已经被谷歌收购.(The Reflexxes Motion Librar ...

  2. V-rep学习笔记:碰撞检测与距离计算

    V-REP可以在几何组件中快速判断各种干扰与碰撞,以及计算多个组件间的最小距离. 碰撞检测 V-REP可以检测两个碰撞体实体(Collidable objects are objects that c ...

  3. CString与UTF8互转代码

    这个代码网上很多,留在这里做个备份. static std::string ConvertCStringToUTF8( CString strValue ) { std::wstring wbuffe ...

  4. 转error while loading shared libraries的解決方法

    error while loading shared libraries的解決方法 者 icq 21:03 | 靜態連結網址 | 迴響 (0) | 引用 (1) | 點閱次數 (270) | Prog ...

  5. code vs 3492 细胞个数

    题目链接:http://codevs.cn/problem/3492/ 题目描述 Description 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为 ...

  6. 微信小程序支付源码,后台服务端代码

    作者:尹华南,来自原文地址 微信小程序支付绕坑指南 步骤 A:小程序向服务端发送商品详情.金额.openid B:服务端向微信统一下单 C:服务器收到返回信息二次签名发回给小程序 D:小程序发起支付 ...

  7. Linux下的二进制兼容性的检测

    都知道Linux的二进制兼容与windows相比简直是天差地别,windows的二进制兼容做得特别好,很老的游戏都能在windows 10上跑,写的好的程序,在XP上也不是不可以.但是Linux就不一 ...

  8. javascript 的 jasmine 的測试语句

    首先建立环境场景: 一般三个文件夹 lib jasmine的系统文件存放文件夹 spec 写測试用例的文件夹 src 存放源码的文件夹(被測对象) specRunner.html 測试入口文件. 入口 ...

  9. 在string资源中添加变量

    public void checkModeByAction() {     if(TxrjConstant.ACTION_ADD_RECIPIENT.equals(getIntent().getAct ...

  10. Xcode使用小技巧-filter查找功能和查看最近修改的文件

    今天偶然发现了关于Xcode的一个小技巧: 1.查看最近修改的文件 2.使用filter查找制定文件 没错,就是下面这个东西,很容易忽略的一个小工具,在Xcode左下角位置. 通过这个,我们能够在整个 ...