nginx配置-最后整理版

nginx_upstream_check_module
nginx-module-vts
nginx打补丁

nginx编译安装

- 下载
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.12.2.tar.gz tar -xf nginx-1.12.2.tar.gz - 准备环境
useradd -s /sbin/nologin -M www
yum install perl-devel openssl-devel gcc -y - 编译安装
cd /usr/local/src/nginx-1.12.2
./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx-1.12.2 /usr/local/nginx
cd /usr/local/nginx/conf
egrep -v '^$|#' nginx.conf.default > nginx.conf echo "PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile
source /etc/profile - 管理命令
nginx -t #检查语法
nginx -s stop
nginx -s reload

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

nginx安全优化

改软件名
改版本号
限制访问(dir/file/扩展名)
限制访问方法
限制上传大小
防盗链
防爬虫
防非法解析 目录安全
使用普通用户运行
www运行worker 错误页优化

改软件名

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

改版本号

- 默认有版本号
[root@n1 ~]# curl -I 192.168.2.11 #获取的是响应头信息
HTTP/1.1 200 OK
Server: **nginx/1.12.2**
... - 修改nginx.conf
http{
...
server_tokens off;
...
} - 再次测试,发现没版本号了
[root@n1 ~]# curl -I 192.168.2.11 HTTP/1.1 200 OK
Server: MTWS #这里的版本号没了
...

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

站点目录及文件访问控制

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

限制访问方法

利用Nginx限制HTTP的请求方法

#Only allow these request methods

    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 501;
} #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的方法如下:
    server {
listen 80;
server_name www.maotai.com;
root html/www;
index index.html index.htm;
access_log logs/www_access.log main; #Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.maotai.com maotai.com;
if ($invalid_referer){
rewrite ^/ http://www.maotai.com/img/nolink.jpg;
}
access_log off;
root html/www;
expires 1d;
break;
}
}

防爬虫

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

    ## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget)
{
return 403;
}

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

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

    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")
{
return 403;
}

防非法解析

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

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

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

目录安全

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

使用普通用户启动nginx

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

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

2、编写配置文件

worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
error_log /home/nginx/blog/logs/error.log;
user inca inca;
pid /home/nginx/blog/logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type usr/local/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 8080;
server_name www.maotai.com;
root /home/nginx/blog/html;
location / {
index index.html index.htm;
}
access_log /home/nginx/blog/logs/web_blog_access.log main;
}
}

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

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

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

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

worker进程以www用户运行

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

错误页优化

NGINX错误页面友好显示

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

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

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

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

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

    error_page 404 =200 /empty.gif;
server {
listen 80;
server_name www.maotai.com;
location / {
root /data0/www/bbs;
index index.html index.htm;
fastcgi_intercept_errors on;
error_page 404 =200 /ta.jpg;
access_log /app/logs/bbs_access.log commonlog;
}
}

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

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

[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. 每天五个java相关面试题(7)--线程篇

    首先关于线程能够先看我之前这篇博文熟悉一下线程生命周期的图示以及文字说明 好開始了: 1.java 中有几种方法能够实现一个线程? 答:有两种实现方法.各自是继承 Thread类与实现Runnable ...

  2. Selenium简单测试页面加载速度的性能(Page loading performance)

    利用selenium的可以执行javascript脚本的特性,我写了一个java版本的获得页面加载速度的代码,这样你就可以在进行功能测试的同时进行一个简单的测试页面的加载速度的性能测试. 我现在的项目 ...

  3. testNG retry 失败的testcase只需要在xml中配置一个listener即可

    问题情况                                                  先说下问题情况,最近在做testNG与selenium集成做自动化测试的问题. 因为如果将t ...

  4. Dynamic Lotusscript

    Introduction This short article gives an introduction to the underrated Execute method that is avail ...

  5. linux 上配置tomcat、mysql 开机启动

    1.tomcat 开机启动 方法一.修改系统文件 (已经尝试,可以) 1.修改/etc/rc.d/rc.local vi /etc/rc.d/rc.local 2.添加下面两行脚本,记住是两行,仅仅第 ...

  6. Ubuntu18.04中配置QT5.11开发环境

    准备工作 参考 https://wiki.qt.io/Install_Qt_5_on_Ubuntu . # 安装g++ sudo apt install build-essential # sudo ...

  7. Java实现List数组的几种替代方案

    在Java中,禁止定义List<Integer>a[],这种List数组结构. 但是还是可以使用其它一些方式来实现列表数组. 一.使用Node把List包裹起来 public class ...

  8. linux shell 脚本攻略学习5---find命令详解

    1.find命令详解 语法: find base_path#base_path可以是任何位置,find会从该位置向下找 实例: amosli@amosli-pc:~$ find /home/amosl ...

  9. 线程同步之mutex和event区别

    之前只是用过 关键段来对同进程不同线程进行互斥,防止对同一份资源或代码段的竞争: mutex可以理解为不同进程或者同一进程内防止对同一份资源的竞争: event更多的是同步,当然也是不同进程或者同一进 ...

  10. C#获取Web和非Web程序的目录

    几种方法如下:非Web程序 1.AppDomain.CurrentDomain.BaseDirectory 2.Environment.CurrentDirectory 3.HttpRuntime.B ...