12.7 Nginx默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向
12.10 Nginx访问日志
12.11 Nginx日志切割
12.12 静态文件不记录日志和过期时间
12.13 Nginx防盗链
12.14 Nginx访问控制
12.15 Nginx解析php相关配置
12.16 Nginx代理
12.17扩展
12.18课堂笔记

12.7 Nginx默认虚拟主机
自定义默认虚拟主机
一、修改nginx配置文件
• vim /usr/local/nginx/conf/nginx.conf 
修改内容如下:
1、将http配置server部分删掉
2、在http部分最后添加一句
 include vhost/*.conf;
二、创建vhost目录
• mkdir /usr/local/nginx/conf/vhost
三、编辑自定义虚拟主机文件
cd /usr/local/nginx/conf/vhost
•  vim aaa.conf 
#加入如下内容
server
{
listen 80 default_server;   #有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa;
}
四、创建指定网站目录
• mkdir -p /data/wwwroot/aaa/
五、测试默认虚拟主机是否配置完成
1、添加内容到/data/wwwroot/default/index.html
• echo “This is a default site.”>/data/wwwroot/aaa/index.html
2、检查配置文件语法并重新加载
• /usr/local/nginx/sbin/nginx -t
• /usr/local/nginx/sbin/nginx -s reload
3、测试
• curl localhost
• curl -x127.0.0.1:80 123.com
[root@xinlinux-03 aaa]# curl localhost
“This is a default site.”
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 123.com
“This is a default site.”
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 hdiag.com
“This is a default site.”
两种方法改为默认虚拟主机
1、在vhost目录下第一个.conf的虚拟主机为默认虚拟主机
2、定义虚拟主机时加上“default_server”字段就是默认虚拟主机
12.8 Nginx用户认证
一、自定义一个虚拟主机配置文件
• vim /usr/local/nginx/conf/vhost/test.com.conf  
#写入如下内容
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
二、生成密码文件
• yum install -y httpd
#如果已经安装Apache则不用下载
• /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
#使用htpasswd -c生成密码文件到/usr/local/nginx/conf/htpasswd,并增加用户xin
[root@xinlinux-03 aaa]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
New password:
Re-type new password:
Adding password for user xin
三、测试配置并重新加载
• /usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
四、创建网站目录test.com
• mkdir /data/wwwroot/test.com
五、测试
• echo “test.com”>/data/wwwroot/test.com/index.html
• curl -x127.0.0.1:80 test.com -I  #状态码为401说明需要验证
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:12:27 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
• curl -uxin:1234 -x127.0.0.1:80 test.com 
#访问状态码变为200
[root@xinlinux-03 aaa]# curl -uxin:1234 -x127.0.0.1:80 test.com
“test.com”
• 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
• 针对目录的用户认证(修改location后面的内容)
location  /admin/
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
[root@xinlinux-03 test.com]# curl  -x127.0.0.1:80 test.com/admin/2.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:46 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@xinlinux-03 test.com]# curl -uxin:1234 -x127.0.0.1:80 test.com/admin/2.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:10 GMT
Content-Type: application/octet-stream
Content-Length: 4
Last-Modified: Wed, 19 Sep 2018 09:17:52 GMT
Connection: keep-alive
ETag: "5ba21440-4"
Accept-Ranges: bytes
针对URL的用户认证
location ~ admin.php
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
12.9 Nginx域名重定向
更改test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite  ^/(.*)$ http://test.com/$1  permanent;
}
#if语句表示主域名为test.com,将匹配的其他域名跳转到主域名
}
#server_name后面支持写多个域名,这里要和httpd的做一个对比
#permanent为永久重定向,状态码为301,如果写redirect则为302
[root@xinlinux-03 test.com]# curl  -x127.0.0.1:80 test1.com/admin/2.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:27:40 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/2.php
[root@xinlinux-03 test.com]# curl  -x127.0.0.1:80 test4.com/admin/2.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:27:47 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
12.10 Nginx访问日志
配置日志格式也在主配置文件
• vim /usr/local/nginx/conf/nginx.conf 
#搜索log_format
日志文件设定参数格式  
$remote_addr 客户端IP(公网IP)
$http_x_forwarded_for 代理服务器的IP
$time_local 服务器本地时间
$host 访问主机名(域名)
$request_uri 访问的url地址
$status 状态码
$http_referer referer
$http_user_agent user_agent
#除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
 access_log /tmp/test.com.log combined_realip;
#这里的combined_realip就是在nginx.conf中定义的日志格式名字
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
curl -x127.0.0.1:80 test.com -I
cat /tmp/test.com.log
[root@xinlinux-03 test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Sep/2018:17:34:16 +0800] test.com "/" 200 "-" "curl/7.29.0"
12.11 Nginx日志切割
一、定义shell 脚本
• vim /usr/local/sbin/nginx_logrotate.sh
#写入如下内容
#! /bin/bash
# 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
#日志时间
logdir="/usr/local/nginx/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
#将日志文件改为后缀加时间
done
/bin/kill -HUP `cat $nginx_pid`
#生成新的日志文件
二、添加一个任务计划
crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
12.12 静态文件不记录日志和过期时间
• 配置如下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      7d;
#定义过期时间
access_log off;
#静态文件不记录off
}
location ~ .*\.(js|css)$
{
expires      12h;
access_log off;
}
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
测试
cd  /data/wwwroot/test.com
vim  1.gif
vim  2.js
curl -x127.0.0.1:80 test.com/1.gif
curl -x127.0.0.1:80 test.com/2.js
curl -x127.0.0.1:80 test.com/1.gifhaksdl
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.gif
gahi
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/2.js
123giouh
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.gifhaksdl
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
[root@xinlinux-03 test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Sep/2018:18:22:42 +0800] test.com "/1.gifhaksdl" 404 "-" "curl/7.29.0"
12.13 Nginx防盗链
•配置如下,可以和上面的配置结合起来
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
 valid_referers none blocked server_names  *.test.com ;
#设置*.test.com的网站为白名单
    if ($invalid_referer) {
        return 403;
}
access_log off;
}
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
测试
curl -e "http://www.baidu.com" -x127.0.0.1:80 -I test.com/1.gif
[root@xinlinux-03 test.com]# curl -e "http://www.baidu.com" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:26:48 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
curl -e "http://www.test.com" -x127.0.0.1:80 -I test.com/1.gif
[root@xinlinux-03 test.com]# curl -e "http://www.test.com" -x127.0.0.1:80-I test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:26:55 GMT
Content-Type: image/gif
Content-Length: 5
Last-Modified: Wed, 19 Sep 2018 09:42:00 GMT
Connection: keep-alive
ETag: "5ba219e8-5"
Expires: Wed, 26 Sep 2018 10:26:55 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
12.14 Nginx访问控制
•需求:访问/admin/目录的请求,只允许某几个IP访问
一、编辑主配置文件 /usr/local/nginx/conf/nginx.conf
配置如下:
location /admin/
{
allow 192.168.233.150;
allow 127.0.0.1;
deny all;
#没有order顺序
}
二、测试
1、创建amdin目录并添加内容到admin/1.html
• mkdir /data/wwwroot/test.com/admin/
• echo “test,test”>/data/wwwroot/test.com/admin/1.html
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
• curl -x127.0.0.1:80 test.com/admin/1.html -I
•curl -x192.168.233.150:80 test.com/admin/1.html -I
•可以匹配正则(这段配置将/upload/目录下所有.php结尾的deny掉)
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/upload/2.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:41:36 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:42:00 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Wed, 19 Sep 2018 09:17:19 GMT
Connection: keep-alive
ETag: "5ba2141f-0"
Accept-Ranges: bytes
•根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
#~加上一个*号"~*"可以忽略大小写
{
return 403;
}
#deny all和return 403效果一样
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
curl -A "Tomatoafghra" -x127.0.0.1:80 test.com  -I
[root@xinlinux-03 test.com]# curl -A "Tomatoafghra" -x127.0.0.1:80 test.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
12.15 Nginx解析php相关配置
配置虚拟主机配置文件
• 配置如下:
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
#fastcgi_pass 用来指定php-fpm监听的地址或者socket,php-fpm定义的是什么,nginx配置文件就要些什么;如果路径写错会出现502状态
#如果路径改成fastcgi_pass  127.0.0.1:9000则变成监听端口
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
vim /data/wwwroot/test.com/1.php
<?php
echo "123456";
?>
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.php
<?php
echo "123456";
?>
[root@xinlinux-03 test.com]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@xinlinux-03 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@xinlinux-03 test.com]# !curl
curl -x127.0.0.1:80 test.com/1.php
123456[root@xinlinux-03 test.com]#
实验:将虚拟主机配置文件sock路径写错漏了fc
1、-t并重载
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
2、curl -x127.0.0.1:80 test.com/1.php -I
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.php -I
HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:55:10 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive
3、查看错误日志
tail /usr/local/nginx/logs/nginx_error.log
2018/09/19 18:55:10 [crit] 1400#0: *49 connect() to unix:/tmp/php-gi.sockfailed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/1.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-gi.sock:", host: "test.com"
#发现提示找不到socket文件
4、更正虚拟主机配置的sock路径,与php_fpm的sock路径保持一致,然后在重试一次
[root@xinlinux-03 test.com]# !curl
curl -x127.0.0.1:80 test.com/1.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:00:56 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
#另一种502状态
vim /usr/local/php-fpm/etc/php-fpm.conf  
#如果监听socket没有在php配置文件定义sock文件的权限666,则会变成默认660权限,状态也会出现502
测试前:
[root@xinlinux-03 test.com]# ll /tmp/php-fcgi.sock
srw-rw-rw- 1 root root 0 9月  19 16:59 /tmp/php-fcgi.sock
修改权限后:
[root@xinlinux-03 test.com]# ll /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 9月  19 19:20 /tmp/php-fcgi.sock
测试:
1、将php-fpm的sock文件权限去掉,-t并重载
/usr/local/pgp-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm  restart
2、curl -x127.0.0.1:80 test.com/1.php  -I
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.php -I
HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:25:57 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive
3、查看sock文件属性
[root@xinlinux-03 test.com]# ll /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 9月  19 19:20 /tmp/php-fcgi.sock
#由nginx的配置文件可以知道是nobody用户启动nginx的
[root@xinlinux-03 test.com]# chown nobody /tmp/php-fcgi.sock
[root@xinlinux-03 test.com]# ll /tmp/php-fcgi.sock
srw-rw---- 1 nobody root 0 9月  19 19:32 /tmp/php-fcgi.sock
[root@xinlinux-03 test.com]# !curl
curl -x127.0.0.1:80 test.com/1.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:34:36 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
4、将权限改回去,-t并重载
/usr/local/pgp-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm  restart
5、curl -x127.0.0.1:80 test.com/1.php -I
[root@xinlinux-03 test.com]# curl -x127.0.0.1:80 test.com/1.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:30:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
12.16 Nginx代理
• cd /usr/local/nginx/conf/vhost
• vim proxy.conf 
#加入如下内容
server
{
listen 80;
server_name ask.apelearn.com;
location /
{
proxy_pass     http://121.201.9.155/;
proxy_set_header Host   $host;
proxy_set_header X-Real-IP   $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
curl -x127.0.0.1:80 ask.apelearn.com/roots.txt
12.17扩展
nginx.conf 配置详解
https://coding.net/u/aminglinux/p/nginx/git/tree/master/3z
nginx rewrite四种flag
http://unixman.blog.51cto.com/10163040/1711943
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md
502问题汇总 http://ask.apelearn.com/question/9109
location优先级 https://coding.net/u/aminglinux/p/nginx/git/blob/master/location/priority.md

12.18课堂笔记

一、LAMP与LNMP的主要差异
LAMP:php主要以模块的形式存在作为Apache的模块(.so文件),Apache通过调用模块去实现调用PHP,Apache处理的是非php的请求;php模块就是解析php代码的;
Apache也可以调用php作为一个独立的服务,并非必须以模块的形式存在于Apache中
LNMP:在LNMP上是一个php-fpm服务
二、php安装
#与LAMP的php安装主要差异是,需要开启php-fpm服务
--enble-fpm    #这个编译参数关系到sbin目录是否存在
#每个服务都会有一个普通用户
#1024以内的端口服务必须用root用户才能启动
三、Nginx安装
#防火墙允许访问80端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
四、扩展
1、nginx.conf全局配置
user nobody;
定义运行nginx服务的用户,还可以加上组,如 user nobody nobody;
worker_processes 1;
定义nginx子进程数量,即提供服务的进程数量,该数值建议和服务cpu核数保持一致。
除了可以定义数字外,还可以定义为auto,表示让系统自动调整。
error_log logs/error.log;
定义错误日志的路径,可以是相对路径(相对prefix路径的),也可以是绝对路径。(错误日志级别默认为error)
该配置可以在此处定义,也可以定义到http、server、location里
error_log logs/error.log notice;
定义错误日志路径以及日志级别.
错误日志级别:常见的错误日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少。
如果不定义默认是error
pid logs/nginx.pid;
定义nginx进程pid文件所在路径,可以是相对路径,也可以是绝对路径。
 
worker_rlimit_nofile 100000;
定义nginx最多打开文件数限制。如果没设置的话,这个值为操作系统(ulimit -n)的限制保持一致。
把这个值设高,nginx就不会有“too many open files”问题了。 
nginx.conf server部分配置
server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点)。
以下为nginx.conf配置文件中server{}部分的内容。
    server {
    listen       80;  //监听端口为80,可以自定义其他端口,也可以加上IP地址,如,listen 127.0.0.1:8080;
    server_name  localhost; //定义网站域名,可以写多个,用空格分隔。
#charset koi8-r; //定义网站的字符集,一般不设置,而是在网页代码中设置。(出现乱码可能是charset koi8-r错了)
#access_log  logs/host.access.log  main; //定义访问日志,可以针对每一个server(即每一个站点)设置它们自己的访问日志。
##在server{}里有很多location配置段
 location / {
        root   html;  //定义网站根目录,目录可以是相对路径也可以是绝对路径。
index  index.html index.htm; //定义站点的默认页。
 }
    #error_page  404              /404.html;  //定义404页面
 # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;  //当状态码为500、502、503、504时,则访问50x.html
location = /50x.html {
        root   html;  //定义50x.html所在路径
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#定义访问php脚本时,将会执行本location{}部分指令
#location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;  //proxy_pass后面指定要访问的url链接,用proxy_pass实现代理。
#}
# pass the PHP scripts to FastCGI server listening on
127.0.0.1:9000
#
#location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;  //定义FastCGI服务器监听端口与地址,支持两种形式,1 IP:Port, 2 unix:/path/to/sockt
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  //定义SCRIPT_FILENAME变量,后面的路径/scripts为上面的root指定的目录
#    include        fastcgi_params; //引用prefix/conf/fastcgi_params文件,该文件定义了fastcgi相关的变量
#}
# deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
#
#location ~ /\.ht {   //访问的url中,以/.ht开头的,如,www.example.com/.htaccess,会被拒绝,返回403状态码。
  #    deny  all;  //这里的all指的是所有的请求。
 #}
}
# another virtual host using mix of IP-, name-, and port-based configuration##server {
#    listen       8000;  //监听8000端口#    listen       somename:8080;  //指定ip:port#    server_name  somename  alias  another.alias;  //指定多个server_name
 
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
# HTTPS server##server {
#    listen       443 ssl;  //监听443端口,即ssl
#    server_name  localhost;
### 以下为ssl相关配置
#    ssl_certificate      cert.pem;    //指定pem文件路径#    ssl_certificate_key  cert.key;  //指定key文件路径
#    ssl_session_cache    shared:SSL:1m;  //指定session cache大小
#    ssl_session_timeout  5m;  //指定session超时时间#    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   //指定ssl协议#    ssl_ciphers  HIGH:!aNULL:!MD5;  //指定ssl算法#    ssl_prefer_server_ciphers  on;  //优先采取服务器算法#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
events配置部分
worker_connections 1024;
定义每个work_process同时开启的最大连接数,即允许最多只能有这么多连接。
accept_mutex on;
当某一个时刻只有一个网络连接请求服务器时,服务器上有多个睡眠的进程会被同时叫醒,这样会损耗一定的服务器性能。
Nginx中的accept_mutex设置为on,将会对多个Nginx进程(worker processer)接收连接时进行序列化,防止多个进程争抢资源。
默认就是on。
multi_accept on;
nginx worker processer可以做到同时接收多个新到达的网络连接,前提是把该参数设置为on。
默认为off,即每个worker process一次只能接收一个新到达的网络连接。
use epoll;
Nginx服务器提供了多个事件驱动器模型来处理网络消息。
其支持的类型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。
* select:只能在Windows下使用,这个事件模型不建议在高负载的系统使用
* poll:Nginx默认首选,但不是在所有系统下都可用
* kqueue:这种方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系统中是最高效的
* epoll: 这种方式是在Linux 2.6+内核中最高效的方式
* rtsig:实时信号,可用在Linux 2.2.19的内核中,但不适用在高流量的系统中
* /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操作系统最高效的方式
* eventport: Solaris 10最高效的方式
http配置部分
MIME-Type
include       mime.types;  //cat conf/mime.types
定义nginx能识别的网络资源媒体类型(如,文本、html、js、css、流媒体等)
default_type  application/octet-stream;
定义默认的type,如果不定义改行,默认为text/plain.
log_format
log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
其中main为日志格式的名字,后面的为nginx的内部变量组成的一串字符串。
access_log logs/access.log main;
定义日志的路径以及采用的日志格式,该参数可以在server配置块中定义。
 
sendfile on;
是否调用sendfile函数传输文件,默认为off,使用sendfile函数传输,可以减少user mode和kernel mode的切换,从而提升服务器性能。
对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。
sendfile_max_chunk 128k;
该参数限定Nginx worker process每次调用sendfile()函数传输数据的最大值,默认值为0,如果设置为0则无限制。
 
tcp_nopush on;
当tcp_nopush设置为on时,会调用tcp_cork方法进行数据传输。
使用该方法会产生这样的效果:当应用程序产生数据时,内核不会立马封装包,而是当数据量积累到一定量时才会封装,然后传输。这样有助于解决网络堵塞问题。
默认值为on。举例:快递员收快递、发快递,包裹累积到一定量才会发,节省运输成本。
keepalive_timeout 65 60;
该参数有两个值,第一个值设置nginx服务器与客户端会话结束后仍旧保持连接的最长时间,单位是秒,默认为75s。
第二个值可以省略,它是针对客户端的浏览器来设置的,可以通过curl -I看到header信息中有一项Keep-Alive: timeout=60,如果不设置就没有这一项。
第二个数值设置后,浏览器就会根据这个数值决定何时主动关闭连接,Nginx服务器就不操心了。但有的浏览器并不认可该参数。
 
send_timeout
这个超时时间是发送响应的超时时间,即Nginx服务器向客户端发送了数据包,但客户端一直没有去接收这个数据包。
如果某个连接超过send_timeout定义的超时时间,那么Nginx将会关闭这个连接。
 
client_max_body_size 10m;
浏览器在发送含有较大HTTP包体的请求时,其头部会有一个Content-Length字段,client_max_body_size是用来限制Content-Length所示值的大小的。
这个限制包体的配置不用等Nginx接收完所有的HTTP包体,就可以告诉用户请求过大不被接受。会返回413状态码。
例如,用户试图上传一个1GB的文件,Nginx在收完包头后,发现Content-Length超过client_max_body_size定义的值,
就直接发送413(Request Entity Too Large)响应给客户端。
gzip on;
是否开启gzip压缩。
gzip_min_length 1k;
设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大。
 
gzip_buffers 4 16k;
设置系统获取几个单位的buffer用于存储gzip的压缩结果数据流。4 16k代表分配4个16k的buffer。
 
gzip_http_version 1.1;
用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户会看到乱码,所以为了支持前期版本加上了这个选项。
如果你用了Nginx反向代理并期望也启用Gzip压缩的话,由于末端通信是http/1.1,故请设置为 1.1。
 
gzip_comp_level 6;
gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
 
gzip_types mime-type ... ;
匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
在conf/mime.conf里查看对应的type。
示例:gzip_types       text/plain application/x-javascript text/css text/html application/xml;
gzip_proxied any;
Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含”Via”的 header头。
以下为可用的值:
off - 关闭所有的代理结果数据的压缩
expired - 启用压缩,如果header头中包含 "Expires" 头信息
no-cache - 启用压缩,如果header头中包含 "Cache-Control:no-cache" 头信息
no-store - 启用压缩,如果header头中包含 "Cache-Control:no-store" 头信息
private - 启用压缩,如果header头中包含 "Cache-Control:private" 头信息
no_last_modified - 启用压缩,如果header头中不包含 "Last-Modified" 头信息
no_etag - 启用压缩 ,如果header头中不包含 "ETag" 头信息
auth - 启用压缩 , 如果header头中包含 "Authorization" 头信息
any - 无条件启用压缩
gzip_vary on;
和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据。
2、Nginx比Apache更高效原理
Apache工作模式:每一个请求过来都是由一个进程或线程连接进行交互。
Nginx的工作模式:nginx采用了模块化、事件驱动、异步、单线程及非阻塞的架构,并大量采用了多路复用及事件通知机制。在nginx中,连接请求由为数不多的几个仅包含一个线程的进程worker以高效的回环(run-loop)机制进行处理,而每个worker可以并行处理数千个的并发连接及请求。
Nginx会按需同时运行多个进程:一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。主进程以root用户身份运行,而worker、cache loader和cache manager均应以非特权用户身份运行。
 
Nginx的请求机制

Nginx之所以可以支持高并发,是因为Nginx用的是异步非阻塞的机制,而Nginx是靠事件驱动模型来实现这种机制的。 
在Nginx的事件驱动模型下,客户端发起的所有请求在服务端都会被标记为一个事件,Nginx会把这些事件收集到“事件收集器”里, 然后再把这些事件交给内核去处理。
阻塞与非阻塞:
阻塞与非阻塞发生在IO调度中,比如内核到磁盘IO。
1、阻塞方式下,进程/线程在获取最终结果之前,被系统挂起了,也就是所谓的阻塞了,在阻塞过程中该进程什么都干不了, 直到最终结果反馈给它时,它才恢复运行状态。
2、非阻塞方式下,进程/线程在获取最终结果之前,并没有进入被挂起的状态,而是该进程可以继续执行新的任务。 当有最终结果反馈给该进程时,它再把结果交给客户端。
举例:依然是酒店前台接待预定酒席电话的案例。 此时角色不再是前台,而是她的查询有无剩余酒席的同事。如果是阻塞方式,该同事在查询有无剩余酒席的过程中,需要傻傻地 等待酒店管理系统给他返回结果,在此期间不能做其他事情。 如果是非阻塞,该同事在等待酒店管理系统给他返回结果这段时间,可以做其他事情,比如可以通知前台剩余酒席的情况。
同步与异步:
同步、异步发生在当客户端发起请求后,服务端处理客户端的请求时。
1、同步机制,是指客户端发送请求后,需要等待服务端(内核)返回信息后,再继续发送下一个请求。
在同步机制中,所有的请求在服务器端得到同步,即发送方和接收方对请求的处理步调是一致的。
2、异步机制,是指客户端发出一个请求后,不等待服务端(内核)返回信息,就继续发送下一个请求。
在异步机制中,所有来自发送方的请求形成一个队列,接收方处理完后再通知发送方。
举例:一家酒店前台,在旺季的高峰时间段会接很多预定酒席的电话。
如果是同步机制情况下,前台每接一个电话后先不挂掉电话,而是去查询有无剩余酒席,查到结果后,告诉客户。
如果是异步机制情况下,前台每接一个预定电话直接回复客户,一会回复,此时前台把查询这件事情交给了另外的同事,
该前台挂掉电话后,继续处理其他客户的事情,当另外的同事查询到结果后再通知给前台,前台再通知客户。
3、CGI,FastCGI,PHP-CGI与PHP-FPM概念(了解进化过程)
Module方式:PHP Module加载方式;是用 LoadModule 来加载 php5_module,就是把php作为apache的一个子模块来运行。当通过web访问php文件时,apache就会调用php5_module来解析php代码。
缺点:并发性低;且把mod_php编进apache时,出问题时很难定位是php的问题还是apache的问题。
CGI:CGI(Common Gateway Interface)全称是“通用网关接口”,CGI就是专门用来和 web 服务器打交道的。web服务器收到用户请求,就会把请求提交给cgi程序(如php-cgi),cgi程序根据请求提交的参数作应处理(解析php),然后输出标准的html语句,返回给web服服务器,WEB服务器再返回给客户端,这就是普通cgi的工作原理。
缺点是fork-and-execute模式
FastCGI:像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行,并且接受来自其它网站服务器来的请求。
PHP-CGI:就是PHP实现的自带的FastCGI管理器。性能太差,而且也很麻烦不人性化,主要体现在:
1. php-cgi变更php.ini配置后,需重启php-cgi才能让新的php-ini生效,不可以平滑重启。
2. 直接杀死php-cgi进程,php就不能运行了。
PHP-FPM :是对于 FastCGI 协议的具体实现,克服了PHP-CGI的两个问题,他负责管理一个进程池,来处理来自Web服务器的请求。目前,PHP5.3版本之后,PHP-FPM是内置于PHP的。
因为PHP-CGI只是个CGI程序,他自己本身只能解析请求,返回结果,不会进程管理。所以就出现了一些能够调度 php-cgi 进程的程序,比如说由lighthttpd分离出来的spawn-fcgi。同样,PHP-FPM也是用于调度管理PHP解析器php-cgi的管理程序。
PHP-FPM通过生成新的子进程可以实现php.ini修改后的平滑重启。

12.7 Nginx默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向
12.10 Nginx访问日志
12.11 Nginx日志切割
12.12 静态文件不记录日志和过期时间
12.13 Nginx防盗链
12.14 Nginx访问控制
12.15 Nginx解析php相关配置
12.16 Nginx代理
12.7 Nginx默认虚拟主机
自定义默认虚拟主机
一、修改nginx配置文件
• vim /usr/local/nginx/conf/nginx.conf 
修改内容如下:
1、将http配置server部分删掉
2、在http部分最后添加一句
 include vhost/*.conf;
二、创建vhost目录
• mkdir /usr/local/nginx/conf/vhost
三、编辑自定义虚拟主机文件
cd /usr/local/nginx/conf/vhost
•  vim aaa.conf 
#加入如下内容
server
{
listen 80 default_server;   #有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa;
}
四、创建指定网站目录
• mkdir -p /data/wwwroot/aaa/
五、测试默认虚拟主机是否配置完成
1、添加内容到/data/wwwroot/default/index.html
• echo “This is a default site.”>/data/wwwroot/aaa/index.html
2、检查配置文件语法并重新加载
• /usr/local/nginx/sbin/nginx -t
• /usr/local/nginx/sbin/nginx -s reload
3、测试
• curl localhost
• curl -x127.0.0.1:80 123.com
[root@xinlinux-03 aaa]# curl localhost
“This is a default site.”
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 123.com
“This is a default site.”
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 hdiag.com
“This is a default site.”
两种方法改为默认虚拟主机
1、在vhost目录下第一个.conf的虚拟主机为默认虚拟主机
2、定义虚拟主机时加上“default_server”字段就是默认虚拟主机
12.8 Nginx用户认证
一、自定义一个虚拟主机配置文件
• vim /usr/local/nginx/conf/vhost/test.com.conf  
#写入如下内容
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
二、生成密码文件
• yum install -y httpd
#如果已经安装Apache则不用下载
• /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
#使用htpasswd -c生成密码文件到/usr/local/nginx/conf/htpasswd,并增加用户xin
[root@xinlinux-03 aaa]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
New password:
Re-type new password:
Adding password for user xin
三、测试配置并重新加载
• /usr/local/nginx/sbin/nginx-t 
/usr/local/nginx/sbin/nginx  -s reload 
四、创建网站目录test.com
• mkdir /data/wwwroot/test.com
五、测试
• echo “test.com”>/data/wwwroot/test.com/index.html
• curl -x127.0.0.1:80 test.com -I  #状态码为401说明需要验证
[root@xinlinux-03 aaa]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:12:27 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
• curl -uxin:1234 -x127.0.0.1:80 test.com 
#访问状态码变为200
[root@xinlinux-03 aaa]# curl -uxin:1234 -x127.0.0.1:80 test.com
“test.com”
• 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
• 针对目录的用户认证(修改location后面的内容)
location  /admin/
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
[root@xinlinux-03 test.com]# curl  -x127.0.0.1:80 test.com/admin/2.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:46 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@xinlinux-03 test.com]# curl -uxin:1234 -x127.0.0.1:80 test.com/admin/2.php -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:10 GMT
Content-Type: application/octet-stream
Content-Length: 4
Last-Modified: Wed, 19 Sep 2018 09:17:52 GMT
Connection: keep-alive
ETag: "5ba21440-4"
Accept-Ranges: bytes
针对URL的用户认证
location ~ admin.php
{
auth_basic              "Auth";
auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

转载于:https://www.cnblogs.com/Lucky-LGX/p/9677601.html

第十二章、LNMP架构(中)相关推荐

  1. Tensorflow 2.x(keras)源码详解之第十二章:keras中的损失函数之BinaryCrossentropy详解

      大家好,我是爱编程的喵喵.双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中.从事机器学习以及相关的前后端开发工作.曾在阿里云.科大讯飞.CCF等比赛获得多次Top名次.现 ...

  2. RH124 第十二章 Linux系统中的软件管理

    RH124 第十二章 Linux系统中的软件管理 1.Linux中软件包的类型 1.DEB #UBlinux DEBlinux 2.RPM #redhat centOS fc 3.bz2|gz|xz ...

  3. 使用java理解程序逻辑 第十二章_Java多线程中锁的理解与使用(二)

    博主将会针对Java面试题写一组文章,包括J2ee,SQL,主流Web框架,中间件等面试过程中面试官经常问的问题,欢迎大家关注.一起学习,一起成长. 独享锁/共享锁 独享锁是指该锁一次只能被一个线程所 ...

  4. 第十二章、Designer中的menu菜单、toolBar工具栏和Action动作

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 Qt Designer中的部件栏并没有菜单.toolBar以及Action相关的部件,仅在 ...

  5. 系统架构师学习笔记_第十二章_连载

    第十二章  系统安全架构设计 12.1  信息系统安全架构的简单描述 信息安全的特征 是为了保证信息的 机密性.完整性.可用性.可控性.不可抵赖性. 以风险策略为基础. 12.1.1  信息安全的现状 ...

  6. 【译】 WebSocket 协议第十二章——使用其他规范中的WebSocket协议

    概述 本文为 WebSocket 协议的第十二章,本文翻译的主要内容为如何使用其他规范中的 WebSocket 协议. 使用其他规范中的WebSocket协议(协议正文) WebSocket协议旨在由 ...

  7. 微型计算机中存储器分成哪几个等级?它们各有什么特点?用途如何?,《微机原理》复习思考题第十二章存储器.DOC...

    <微机原理>复习思考题第十二章存储器 第章 存储器 ?????????????????????????????????????????????????????????? ????????? ...

  8. 唐 库利超级计算机,第七卷 乖离性 百万亚瑟王_第二百五十二章 绝望中的希望...

    第七卷 乖离性 百万亚瑟王_第二百五十二章 绝望中的希望 赫布里底训练大厅. "诸位,结果已经分析出来了,丘库林,也就是被你们捉到的闯入者,他身上的神装的确是断绝时代的遗物不错,而且,这件神 ...

  9. Python在机器学习中的应用--第十二章深度学习

    第十二章深度学习 import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import Font ...

  10. 《深入理解 Spring Cloud 与微服务构建》第十二章 服务注册和发现 Consul

    <深入理解 Spring Cloud 与微服务构建>第十二章 服务注册和发现 Consul 文章目录 <深入理解 Spring Cloud 与微服务构建>第十二章 服务注册和发 ...

最新文章

  1. 沈阳初步建成国内领先的城市级大数据资源库
  2. 服务器版Windows7系统,windows7服务器版本
  3. Python学习笔记之字典(一)
  4. mappedbytebuffer_小师妹学JavaIO之:MappedByteBuffer多大的文件我都装得下
  5. 如何在Sitecore CMS中打开内容编辑器
  6. 【优化算法】符号LMS算法【含Matlab源码 697期】
  7. 史诗级互联网电商系统的演进过程详解
  8. SSLOJ·马蹄印【DFS】
  9. linux循环显示系统时间,Linux shell时间处理大全,值得收藏
  10. 十四、理解nn.module方法——学习python面向对象编程(一)
  11. rtx3090ti什么水平 rtx3090ti显卡什么级别 rtx3090ti显卡怎么样
  12. 项目一(基础)家庭记录收支记账软件
  13. docker部署环境
  14. MongoDB 启动参数
  15. 计算机硬技术与基础在线测试,计算机硬件技术基础网上作业及答案
  16. OC textField键盘弹起事件
  17. 百问网七天物联网课程学习笔记——单片机开发模式
  18. 2017 阿里巴巴矢量图标库 导入所选图标到jsp页面步骤
  19. 无线电台、无线网桥、网络结构相关知识总结
  20. 对于iPhone5分辨率兼容性调整问题

热门文章

  1. 音乐u盘排序软件_传输数据快速的各种U盘系列 定制U盘
  2. 期末C语言45分能过的吗,C语言—期末小黄衫获奖感言
  3. h2 不能访问localhost_Spring 配置的 H2 控制台 frameOptions 导致无法访问
  4. 手动制造报错_Windows 10驱动更新调整:不再自动安装“手动”驱动更新
  5. H5+个推实现消息推送服务
  6. 小程序用php的优势,小程序的特点及优势?
  7. serviceFabric oracle,Oracle ASM和多路径软件的兼容性【转】
  8. 贝叶斯集锦:贝叶斯统计基础
  9. python gui控件案例_python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例...
  10. Python socket编程模拟最简单的HTTP响应