Linux-LNMP-Nginx配置二

  • 静态文件不记录日志和过期时间
  • Nginx防盗链
  • Nginx访问控制
  • Nginx解析php相关配置
  • Nginx代理
  • Nginx负载均衡
  • SSL原理
  • 生成SSL密钥对
  • Nginx配置SSL

静态文件不记录日志和过期时间
在Nginx服务器的虚拟主机配置文件(/usr/local/nginx/conf/vhost/norecord.conf)中定义

vim /usr/local/nginx/conf/vhost/norecord.conf

文件内容如下:

server
{listen 80; server_name www.norecord.com;index index.html index.htm index.php;root /data/wwwroot/norecord;location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 7d;access_log off;}access_log /tmp/norecord.log combined_realip;
}

创建测试目录,测试文件

mkdir -p /data/wwwroot/norecord
cd /data/wwwroot/norecord/
echo "record_log" > index.html
rz  //从本地上传一张123.jpg图片文件到当前目录下

查看图片和文件

检测语法,加载配置文件

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

测试

curl -x127.0.0.1:80 www.norecord.com www.norecord.com/123.jpg -I

有这要的标志(有7天的时间限制)

查看日志

cat /tmp/norecord.log


发现访问图片没有记录日志


Nginx防盗链
实验:仅允许来自这个Referer(https://blog.51cto.com/13480443/2074160) 的链接访问Nginx服务器的www.burglar.com 下以.(gif|jpeg|png|bmp|swf)结尾的文件并且不记录日志
配置虚拟主机

server
{listen 80;server_name www.burglar.com;index index.html index.htm index.php;root /data/wwwroot/burglar;location ~* ^.+\.(gif|jpg|jpeg|png|bmp|swf)${expires 7d;valid_referers server_names "blog.51cto.com/13480443/2074160";if ($invalid_referer){return 403;}access_log off;}access_log /tmp/burglar.log combined_realip;}

valid_referers none blocked server_name *.test.com //表示匹配空referer或以.test.com结尾的referer


Nginx访问控制

限制某个来源ip访问Nginx服务器上的某个目录
server
{listen 80; server_name www.accesscontrol.com www.accesscontrol1.com www.accesscontrol2.com;index index.html index.htm index.php;root /data/wwwroot/accesscontrol;location /admin/{   allow 192.168.221.20;allow 127.0.0.1;deny all;}
}

限制某些来源ip访问Nginx服务器上的某个文件

location ~ .*(upload|image)/.*\.php${deny all;}

可以根据user_agent来进行限制(~ 匹配区分大小写,~* 匹配不区分大小写)

if ($http_user_agent ~* 'Mozilla|curl|Spider/3.0|YoudaoBot|Tomato' ){return 403;}

Nginx是从上往下进行匹配的,如果匹配到就不再往下匹配


Nginx解析php相关配置

vim /usr/local/nginx/conf/vhost/analysis.conf
server{listen 80;server_name www.analysis.com www.analysis1.com www.analysis2.com;index index.html index.htm index.php;root /data/wwwroot/analysis;location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwroot/analysis$fastcgi_script_name;}    }

如果下面这行写错了,就会显示返回状态码502(找不到/tmp/php-fcgi.sock)


查询错误日志(/usr/local/nginx/logs/nginx_error.log)

查询php配置文件中定义的"listen"

grep listen /usr/local/php-fpm/etc/php-fpm.conf
listen = /tmp/php-fcgi.sock
listen.mode = 666

如果将php-fpm服务监听ip,重新加载php-fpm服务

vim /usr/local/php-fpm/etc/php-fpm.conf
listen=127.0.0.1:9000
/etc/init.d/php-fpm reload

再次访问还是返回状态码502
将nginx虚拟主机的配置文件改为监听ip和端口的形式

vim /usr/local/nginx/conf/vhost/analysis.conf
fastcgi_pass 127.0.0.1:9000;

检测Nginx配置文件的语法错误,重新加载配置文件,就ok了。
如果将/usr/local/php-fpm/etc/php-fpm.conf文件中的listen.mode = 666注释了,php配置文件与nginx虚拟主机中定义都是sock的,则监听的文件/tmp/php-fcgi.sock的默认权限为660,访问时返回的状态码依然是502
查看错误日志

临时将/tmp/php-fcgi.sock文件中的属主改为nobody,访问又ok了

chown nobody /tmp/php-fcgi.sock


Nginx代理
在代理服务器上作如下设置

vim /usr/local/nginx/conf/vhost/proxy.conf
server
{listen 80;server_name www.qq.com;location /{proxy_pass http://182.254.74.167/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}

测试

curl -x127.0.0.1:80 www.qq.com

Nginx负载均衡
dig命令

yum install bind-utils -y
dig baidu.com  //返回了三个ip

配置文件中定义

vim /usr/local/nginx/conf/vhost/balance.conf
upstream baidu
{ip_hash;server 111.13.101.208;server 220.181.57.216;server 123.125.114.144;
}
server
{listen 80;server_name www.baidu.com;location /{proxy_pass http://baidu;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
curl -x127.0.0.1:80 www.baidu.com

SSL原理
客户端发送https请求给服务器。
服务器有一套证书(公钥和私钥),将公钥传递给客户端
客户端收到公钥后,验证合法性,无效则会警告提醒,有效则会生成一串随机字符,用公钥加密码随机字符传递给服务器
服务器用自己的私钥解密,得到明文的字符串,用字符串加密数据传给客户端
客户端用自己的字符串解密得到明文数据。


生成SSL密钥对

openssl genrsa -des3 -out tmp.key 2048  //输入复杂密码2次,tmp.key为私钥
openssl rsa -in tmp.key -out apeng.key    //输入上面的密码一次,转换key,取消密码
rm -f tmp.key
openssl req -new -key apeng.key -out apeng.csr  //生成证书请求文件apeng.csr
openssl x509 -req -days 365 -in apeng.csr -signkey apeng.key -out apeng.crt  //apeng.csr和私钥apeng.key生成apeng.crt公钥

Nginx配置SSL

vim /usr/local/nginx/conf/vhost/ssl.conf
server
{listen 443;server_name www.ssl.com;index index.html index.htm index.php;root /data/wwwroot/ssl;ssl on;ssl_certificate apeng.crt;ssl_certificate_key apeng.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;location ~ \.php${include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwroot/ssl$fastcgi_script_name;}
}



重新编译nginx

./configure --help|grep -i ssl
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install

/etc/init.d/nginx restart
netstat -tlnp

mkdir -p /data/wwwroot/ssl
echo "ssl" > /data/wwwroot/ssl/index.html
vim /etc/hosts
192.168.221.20 www.ssl.com
curl https://www.ssl.com
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.htmlcurl performs SSL certificate verification by default, using a "bundle"of Certificate Authority (CA) public keys (CA certs). If the defaultbundle file isn't adequate, you can specify an alternate fileusing the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented inthe bundle, the certificate verification probably failed due to aproblem with the certificate (it might be expired, or the name mightnot match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, usethe -k (or --insecure) option.

浏览器访问

iptables -I INPUT -p tcp --dport 443 -j ACCEPT


转载于:https://blog.51cto.com/13480443/2074160

Linux-LNMP(静态元素不记录日志和过期时间,防盗链,解析php,代理,支持ssl)相关推荐

  1. Nginx访问日志、日志切割、静态文件不记录日志和过期时间

    2019独角兽企业重金招聘Python工程师标准>>> 11月27日任务 12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间 1 ...

  2. 12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间

    - 12.10 Nginx访问日志 - 12.11 Nginx日志切割 - 12.12 静态文件不记录日志和过期时间# 12.10 Nginx访问日志 - 日志的格式- vim /usr/local/ ...

  3. 12.12 静态文件不记录日志和过期时间

    2019独角兽企业重金招聘Python工程师标准>>> 静态文件不记录日志和过期时间目录概要 配置如下 location ~ .*\.(gif|jpg|jpeg|png|bmp|sw ...

  4. lnmp/nginx系统真正有效的图片防盗链完整设置详解

    http://www.it300.com/article-15345.html 关于nginx防盗链的方法网上有很多教程,都可以用,但是我发现很多教程并不完整,所做的防盗链并不是真正的彻底的防盗链! ...

  5. Linux centos7 VMware Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间...

    一.Apache访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 vim /usr/local/apache2.4/conf/extra/httpd-vho ...

  6. 预习:11.16/11.17 Apache默认虚拟主机-11.24 静态元素过期时间

    预习: 11.16/11.17 Apache默认虚拟主机 11.18 Apache用户认证 11.19/11.20 域名跳转 11.21 Apache访问日志 11.22 访问日志不记录静态文件 11 ...

  7. 设置日志不记录指定类型的文件,日志文件的切割,配置静态元素过期时间

    访问日志不记录指定类型的文件 修改虚拟主机配置文件 先不重新加载配置,测试访问jpg文件,是否记录日志 测试结果是还是记录了jpg文件的日志 -t graceful重新加载一次,就会发现不会记录jpg ...

  8. 访问日志不记录静态文件、访问日志切割、静态元素过期时间

    11.22 访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 小技巧: 打开浏览器,按键盘上的F12键,开发人员工具,选择Network选项(一般默认),刷 ...

  9. 4.16访问日志不记录静态文件,访问日志切割以及静态元素过期时间

    访问日志不记录静态文件 一个网站会有很多元素,尤其是图片.js.css等静态文件非常多,每个用户请求一个页面都会访问诸多的图片,这些元素都会被记录在日志中,如果一个网站访问量很大,那么这些日志会增长的 ...

最新文章

  1. 有一份华为荣誉证书等你来拿!网络人工智能硬盘异常预测黑客松比赛火热报名中...
  2. 不同stm32f103芯片内部外设资源
  3. 洛谷——1064金明的预算方案————有依赖的背包
  4. 线程池三种创建方式和自定义线程池ThreadPoolExecutor
  5. 10-20-010-简介-目录-Kylin目录详解
  6. HDU3348 coins【贪心】
  7. 安卓手机管理软件_日程管理软件哪个好?
  8. 深度学习2.0-14.神经网络与全连接层之全连接层、输出方式、误差计算
  9. 浙江大学-英特尔嵌入式技术中心成立
  10. 权御天下计算机音乐数字乐谱,权御天下(单音 适合电吉他)
  11. net core获取网站运行目录
  12. VS C++ 重新编译
  13. 普林斯顿微积分读本05第四章--求解多项式的极限问题
  14. 打印水仙花数,并统计个数 java 代码
  15. stata两种方法查看命令源代码
  16. python全套教程百度网盘-Python最新全套视频教程百度网盘资源
  17. 鸿蒙落 万物生(上)
  18. 【SAS应用统计分析】方差分析
  19. python sklearn svm_文本分类和预测 sklearn.svm.LinearSVC(1)
  20. Signal TapII 软件的使用

热门文章

  1. vs中将网站aspx.cs文件打包成一个dll
  2. Fedora 14下安装使用rarlinux
  3. Git学习记录(一)
  4. 解决安卓系统写入SD卡权限问题
  5. Flex报错Error #2048: 安全沙箱冲突
  6. 禁止validateRequest的办法
  7. 人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码
  8. 【Qt】Qt再学习(二):Bars Example(Q3DBars)
  9. 【H2645】帧间预测
  10. 和dump文件_SRA数据库及下载二代测序原始数据转换为fastq文件