深度优化LNMP之Nginx [2]
深度优化LNMP之Nginx [2]
配置Nginx gzip 压缩实现性能优化
- gzip on;#开启gzip压缩功能
- gzip_min_length 1k;
- #设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取,默认值是0,表示不管页面多大都进行压缩,建议设置成大于1K,如果小于1K可能会越压越大
- gzip_buffers 416k;
- #压缩缓冲区大小,表示申请4个单位为16K的内存作为压缩结果流缓存,默认是申请与原始是数据大小相同的内存空间来存储gzip压缩结果;
- gzip_http_version 1.1
- #压缩版本(默认1.1 前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP压缩,使用默认即可。
- gzip_comp_level 2;
- #压缩比率,用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也消耗CPU资源
- gzip_types text/css text/xml application/javascript;
- #用来指定压缩的类型,“text/html”类型总是会被压缩,这个就是HTTP原理部分讲的媒体类型。
- gzip_vary on;
- #vary hear支持,该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用缓存经过Nginx压缩的数据。
- http{
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 32k;
- gzip_http_version 1.1;
- gzip_comp_level 9;
- gzip_types text/css text/xml application/javascript;
- gzip_vary on;
- }


配置Nginx expires 缓存实现性能优化
- location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 3650d;
- }

- [root@web02 /]# curl -I www.jd.com
- HTTP/1.1200 OK
- Server: jdws
- Date:Mon,25Jul201615:15:47 GMT
- Content-Type: text/html; charset=gbk
- Content-Length:197220
- Connection: keep-alive
- Vary:Accept-Encoding
- Expires:Mon,25Jul201615:15:38 GMT #告诉用户什么时候过期
- Cache-Control: max-age=20
- ser:6.158
- Via: BJ-M-YZ-NX-74(HIT), http/1.1 BJ-UNI-1-JCS-117([cRs f ])
- Age:16
- location ~^/(images|javascript|js|css|flash|media|static)/{
- expires 360d;
- }
- HTTP/1.1200 OK
- Server: JDWS
- Date:Mon,25Jul201616:00:32 GMT
- Content-Type: text/html; charset=gbk
- Vary:Accept-Encoding
- Expires:Mon,25Jul201616:00:48 GMT #<==缓存的过期时间
- Cache-Control: max-age=20 #<==缓存的总时间按秒,单位
- ser:130.29
- Via: BJ-Y-NX-104(HIT), http/1.1 HK-1-JCS-70([cRs f ])
- Age:14
- Content-Length:197220
Nginx日志相关优化与安装
- [root@web02 /]# mkdir /server/scripts/-p
- [root@web02 /]# cd /server/scripts/
- [root@web02 scripts]# cat cut_nginx_log.sh
- cd /application/nginx/logs && \
- /bin/mv www_access.log www_access_$(data +%F -d -1dy).log #将日志按日志改成前一天的名称
- /application/nginx/sbin/nginx -s reload #重新加载nginx使得重新生成访问日志文件
- [root@web02 scripts]# crontab -e
- ###cut nginx access log
- 0000***/bin/sh /server/scripts/cut_nginx.log.sh >/dev/null2>&1
- location ~.*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)?$ {
- access_log off;
- }
- chown -R root.root /app/logs/
- chmod -R 700/app/logs
Nginx站点目录及文件URL访问控制
- location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)$
- {
- deny all;
- }
- location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)$
- {
- deny all;
- }
- location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$
- {
- deny all;
- }
- location ~*\.(txt|doc)${
- if(-f $request_filename){
- root /data/www/www;
- #rewrite ....可以重定向某个URL
- break;
- }
- }
- location ~*\.(txt|doc)${
- root /data/www/www;
- deny all;
- }

- location ~ ^/(static)/ {
- deny all;
- }
- location ~ ^/static {
- deny all;
- }
- location ~ ^/(static)/ {
- deny all;
- }
- location /admin/{return404;}
- location /templates/{return403;}
- location ~ ^/docker/ {
- allow 202.111.12.211;
- deny all;
- }
解答:可以,具体方法如下。
- 方法1:使用if来控制。
- if ( $remote_addr = 10.0.0.7 ) {
- return 403;
- }
- if ( $remote_addr = 218.247.17.130 ) {
- set $allow_access_root 'true';
- }
配置Nginx禁止非法域名解析访问企业网站
- server {
- listen 80 default_server;
- server_name _;
- return 501;
- }
- server {
- listen 80 default_server;
- server_name _;
- rewrite ^(.*) http://blog.etiantian.org/$1 permanent;
- }
- if ($host !~ ^www/.oldboyedu/.com$){
- rewrite ^(.*) http://www.oldboyedu.com$1 permanent;
- }
Nginx图片及目录防盗链解决方案


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




- #Preventing hot linking of images and other file types
- location ~* ^.+\.(jpg|png|swf|flv|rar|zip)$ {
- valid_referers none blocked *.abcdocker.org abcdocker.org;
- if ($invalid_referer) {
- rewrite ^/ http://bbs.abcdocker.org/img/nolink.gif;
- }
- root html/www;
- }
jpg|png|swf|flv|rar|zip 表示对jpg、gif等zip为后缀的文件实行防盗链处理 |
*.abcdocker.org abcdocker.org 表示这个请求可以正常访问上面指定的文件资源 |
if{}中内容的意思是:如果地址不是上面指定的地址就跳转到通过rewrite指定的地址,也可以直接通过retum返回403错误 |
return 403为定义的http返回状态码 |
rewrite ^/ http://bbs.abcdocker.org/img/nolink.gif;表示显示一张防盗链图片 |
access_log off;表示不记录访问日志,减轻压力 |
expires 3d指的是所有文件3天的浏览器缓存 |
- <html>
- <head>
- <title>
- </title>
- </head>
- <bodybgcolor=green>
- 博客<br>我的博客<ahref="http://oldboy.blog.etiantian.org" target=_blank“>博客地址</a>
- <imgsrc="http://www.abcdocker.com/stu.jpg">
- </body>
- </html>
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- 10.0.0.1--[30/May/2016:11:13:38+0800]"GET /stu.jpg HTTP/1.1"20068080"http://blog.abcdocker.com/123.html "Mozilla/5.0(Windows NT 6.1; WOW64; rv:47.0)Gecko/20100101Firefox/47.0"
- #Preventing hot linking of images and other file types
- location ~*^.+\.(jpg|png|swf|flv|rar|zip)$ {
- valid_referers none blocked *.abcdocker.org abcdocker.org;
- if($invalid_referer){
- rewrite ^/ http://bbs.abcdocker.org/img/nolink.gif;
- }
- root html/www;
- }

- server {
- listen 80;
- server_name www.etiantian.org;
- location /{
- root html/www;
- index index.php index.html index.htm;
- error_page 404 /404.html
- #当页面出现404错误时,会跳转404.html页面显示给用户
- }
- error_page 404 /404.html;
- error_page 403 /403.html;
- error_page 404 http://oldboy.blog.51cto.com;
- #error_page 404 /404.html;
- error_page 404 http://oldboy.blog.51cto.com;
- error_page 404 /404.html;
- error_page 500 502 503 504 /50x.html;
- error_page 500 501 502 503 504 http://err.tmall.com/error2.html;
- error_page 400 403 404 405 408 410 411 412 413 414 415 http://err.tmall.com/error1.html;
Nginx站点目录文件及目录权限优化
- -rw-r--r-- 1 root root 20May2612:04 test_info.php
- drw-r--r-- 8 root root 4096May2916:41 uploads


Nginx防爬虫优化



- ## Block download agents ##
- if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
- return 403;
- }
- 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;
- }
- if ($http_user_agent ~* "Firefox|MSIE")
- {
- rewrite ^(.*) http://blog.etiantian.org/$1 permanent;
- }
- [root@web02 conf]# cat fastcgi_params
- 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 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 HTTPS $https if_not_empty;
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
- 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;
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- fastcgi_param REDIRECT_STATUS 200;
利用Nginx限制HTTP的请求方法
- #Only allow these request methods
- if ($request_method !~ ^(GET|HEAD|POST)$ ) {
- return 501;
- }
- #Do not accept DELETE, SEARCH and other methods
- ## Only allow GET request methods ##
- if ($request_method ~* ^(GET)$ ) {
- return 501;
- }

使用CDN做网站内容加速
Nginx程序架构优化
使用普通用户启动Nginx(监牢模式)
- [root@web02 ~]# ps -ef|grep nginx
- root 2155 1 003:43? 00:00:00 nginx: master process /application/nginx/sbin/nginx
- www 2156 2155 003:43? 00:00:01 nginx: worker process
- www 3047 2155 006:17? 00:00:00 nginx: worker process
- www 3051 2155 006:17? 00:00:00 nginx: worker process
- www 3435 2155 011:13? 00:00:00 nginx: worker process
- [root@web02 ~]# useradd inca
- [root@web02 ~]# su - inca
- [inca@web02 ~]$ pwd
- /home/inca
- [inca@web02 ~]$ mkdir conf logs www
- [inca@web02 ~]$ cp /application/nginx/conf/mime.types ~/conf/
- [inca@web02 ~]$ echo inca >www/index.html
- [inca@web01 ~]$ cat conf/nginx.conf
- worker_processes 4;
- worker_cpu_affinity 0001 0010 0100 1000;
- worker_rlimit_nofile 65535;
- error_log /home/inca/logs/error.log;
- user inca inca;
- pid /home/inca/logs/nginx.pid;
- events {
- use epoll;
- worker_connections 10240;
- }
- http {
- include mime.types;
- default_type application/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"';
- #web.fei fa daolian..............
- server {
- listen 8080;
- server_name www.etiantian.org;
- root /home/inca/www;
- location / {
- index index.php index.html index.htm;
- }
- access_log /home/inca/logs/web_blog_access.log main;
- }
- }
- [root@web02 ~]#/application/nginx/sbin/nginx -s stop
- [root@web02 ~]# lsof -i:80
- [root@web02 ~]# su - inca
- [inca@web02 ~]$ /application/nginx/sbin/nginx -c /home/inca/conf/nginx.conf &>/dev/null&
- [1]3926
- [inca@web02 ~]$ lsof -i:80
- [1]+ Exit1 /application/nginx/sbin/nginx -c /home/inca/conf/nginx.conf &>/dev/null
<
h1 style=”line-height: 19.2px;”>控制Nginx并发连接数
- limit_conn_zone参数:
- 语法:limit_conn_zone key zone=name:size;
- 上下文:http
- 用于设置共享内存区域,key可以是字符串,nginx自有变量或前两个组合,如$binary_remote_addr、$server_name。name为内存区域的名称,size为内存区域的大小。
- limit_conn参数:
- 语法:limit_conn zone number;
- 上下文:http、server、location
- [root@oldboy ~]# cat /application/nginx/conf/nginx.conf
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- limit_conn_zone $binary_remote_addr zone=addr:10m;
- server {
- listen 80;
- server_name www.etiantian.org;
- location / {
- root html;
- index index.html index.htm;
- limit_conn addr 1; #<==限制单IP的并发连接为1
- }
- }
- }
- location /download/ {
- limit_conn addr 1;
- }

<
h1 style=”line-height: 19.2px;”>控制客户端请求Nginx的速率
- [root@oldboy ~]# cat /application/nginx/conf/nginx.conf
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
- #<==以请求的客户端IP作为key值,内存区域命名为one,分配10m内存空间,访问速率限制为1秒1次请求(request)
- server {
- listen 80;
- server_name www.etiantian.org;
- location / {
- root html;
- index index.html index.htm;
- limit_req zone=one burst=5;
- #<==使用前面定义的名为one的内存空间,队列值为5,即可以有5个请求排队等待。
- }
- }
- }
深度优化LNMP之Nginx [2]的更多相关文章
- 深度优化LNMP之Nginx (转)
深度优化LNMP之Nginx Nginx基本安全优化 1.调整参数隐藏Nginx版本号信息 一般来说,软件的漏洞都和版本有关,因此我们应尽量隐藏或清除Web服务队访问的用户显示各类敏感信息(例 ...
- 深度优化LNMP之PHP (转)
深度优化LNMP之PHP PHP缓存加速介绍 1.操作码介绍及缓存原理 当客户端请求一个php程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate ...
- 深度优化LNMP
优化前准备工作 Centos准备及配置 准备安装包及软件:http://pan.baidu.com/s/1chHQF 下载解压到U盘即可安装http://pan.baidu.com/s/15TUWf ...
- 深度优化LNMP之PHP
PHP缓存加速介绍 1.操作码介绍及缓存原理 当客户端请求一个php程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码文件(Operate Code,opcode)该文 ...
- 深度优化LNMP之MySQL
MySQL数据库优化框架体系 1.硬件层面优化 2.操作系统层面优化 3.MySQL数据库层面优化 4.MySQL安全优化 5.网站集群架构上的优化 6.MySQL流程.制度控制优化 1 硬件层面优化 ...
- 四十三、LAMP与LNMP web架构深度优化实战-第二部
1. 配置nginx gzip压缩功能 服务器对发出的内容进行压缩,带宽少了,体验好,速度快,但是服务端压,会使cpu使用高,压缩比高的进行压缩:文本.程序文件.数据文件.图片视频不要压缩,一般 ...
- 四十二、LAMP与LNMP web架构深度优化实战-第一部
1.nginx.conf配置文件基本参数优化 1.1 隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称及版本等信 ...
- [sql]大型网站MySQL深度优化揭秘
大型网站MySQL深度优化揭秘 第1章优化的思路和线路 1.1 网站优化的思路 2 1.2 MySQL优化,nginx这样的东西怎么优化? 第2章硬件层面优化 2.1 数据库物理机 2.1.1 ...
- CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)
关于 Nginx (发音 “engine x”)这是一款免费.开源.高效的 HTTP 服务器,Nginx是以稳定著称,丰富的功能,结构简单,低资源消耗.本教程演示如何在CentOS 6.5服务器(适用 ...
随机推荐
- 问题-关于sharemem程序访问WEB出现内存错误处理
[delphi技术] 关于sharemem造成dll错误的处理办法问题现象:如果程序和dll之间用string作为参数传递时容易出现错误问题处理:需要在程序的uses中使用sharemem.这个sha ...
- 按需讲解之Supervisor
Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...
- 【推荐】JAVA基础◆浅谈3DES加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 关于IOS中UIWebView 加载HTML内容
NSString *strContent=[info objectForKey:@"newContent"]; { NSArray *paths = NSSearchPathFor ...
- js 数组详解(javascript array)
Array Array 对象用于在单个的变量中存储多个值. 构造函数: 1) new Array(); 2) new Array(size); 3) new Array(element0, ...
- jquery ajax异步加载table的方法
//显示详细信息 function showInfo(actionId, type) { $.post("Sys_Ajax/Sys_EmployInfo.ashx", { &quo ...
- Java中各种排序算法
package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treero ...
- mysql init_connect 参数的其他用处
http://blog.itpub.net/133735/viewspace-691196/ init_connect 是可以动态在线调整的,这样就有了一些其他的用处 经过测试init_conne ...
- Winform之跨线程访问控件(在进度条上显示字体)
此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理 有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...
- Prepare a Nexus Maven private repository
1. port nat mapping #ssh VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port_5002 ...