12.7 默认虚拟主机
- 介绍 现在一个主机可以跑多个站点,所以就有了虚拟主机的概念。我们可以把一台服务器虚拟出多个主机出来,专业就可以实现一台服务器上跑多个站点。
- 作用:任何一个域名指向这台服务器,只要是没有对应的虚拟主机,就会由这个默认虚拟主机来处理。
- 配置:跟httpd类似,第一个Nginx加载的虚拟主机就是默认主机。但和httpd不相同的地方是:Nginx还有一个配置用来标记默认虚拟主机。也就是说,如果没有这个标记,第一个虚拟主机为默认虚拟主机。
## 修改nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf
\\在结束符号}上面加入一行配置,改写如下:
include vhost/*.conf;\\/usr/local/nginx/conf/vhost/下面的所有以.conf结尾的文件都会加载,这样就可以把所有虚拟主机配置文件放到vhost目录下面了。
## 创建并修改虚拟主机配置文件(默认虚拟主机)
mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost
vim default.conf
server
{
// 指定监听80端口,并将该虚拟主机设置为默认虚拟主机
listen 80 default_server;
// 设置服务器的名称
server_name aaa.com;
// 设置服务器默认网页
index index.html index.htm index.php;
// 设置服务器的根目录
root /data/nginx/default;
}
## 创建默认虚拟主机的根目录及默认页面
echo "default_server" > /data/nginx/default/index.html//创建索引页
## 检测代码并重启服务
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
## 检测是否成功
curl -x 127.0.0.1:80 aaa.com
default_server
curl -x 127.0.0.1:80 123.com
default_server\\访问一个没有定义过的域名,也会访问到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; //用户名密码文件目录
}
}
## 创建目录
mkdir /data/nginx/test.com
vim /data/nginx/test.com/index.html
test.com
## 生成密码文件(使用apache的生成密码工具htpasswd)
yum install -y httpd\\安装httpd,也可以使用之前编译安装的apache2.4
htpasswd -c /usr/local/nginx/conf/htpasswd test
New password:
Re-type new password:
Adding password for user test
## 检测并重新加载
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
## 测试
### // 不指定用户名密码访问
url -x 127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Sun, 31 Dec 2017 06:55:24 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
\\状态码401说明,该网站需要验证。
### // 指定用户名密码访问
curl -x 127.0.0.1:80 -utest:testdl991124 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sun, 31 Dec 2017 06:55:33 GMT
Content-Type: text/html
Content-Length: 8
Last-Modified: Sun, 31 Dec 2017 06:17:09 GMT
Connection: keep-alive
ETag: "5a4880e5-8"
Accept-Ranges: bytes
## 针对虚拟主机下的某个目录进行认证
修改配置文件
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/www/test.com;
// 修改location即可,其他都不变
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
## 检测并重新加载
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -t
## 检测
### // test.com可以访问
curl -x 127.0.0.1:80 test.com
test.com
### // test.com下的admin目录需要用户认证
curl -x 127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
12.9 Nginx域名重定向
## 编辑虚拟主机配置文件
vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
## 检错与重新加载
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
## 测试
curl -x127.0.0.1:80 test1.com/1.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 13:41:57 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/1.html
12.10 Nginx访问日志
Nginx访问日志(access_log)介绍
Nginx会把每个用户访问往咱的日志信息记录到指定的日志文件里,供网站管理员分析用户浏览行为等
访问日志参数
Nginx访问日志主要有两个参数控制
log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log #用来指定日至文件的路径及使用的何种日志格式记录日志
lof_format的默认值:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log的默认值:
access_log logs/access.log main;
log_format语法格式及参数语法说明如下:
log_format <NAME> <String>; 关键字 格式标签 日志格式
关键字:其中关键字error_log不能改变
格式标签:格式标签是给一套日志格式设置一个独特的名字
日志格式:给日志设置格式
log_format格式变量: $remote_addr #记录访问网站的客户端地址 $remote_user #远程客户端用户名 $time_local #记录访问时间与时区 $request #用户的http请求起始行信息 $status #http状态码,记录请求返回的状态码,例如:200、301、404等 $body_bytes_sent #服务器发送给客户端的响应body字节数 $http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。 $http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等 $http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置 ##access_log语法格式及参数语法说明如下: access_log <FILE> <NAME>; 关键字 日志文件 格式标签
关键字:其中关键字error_log不能改变
日志文件:可以指定任意存放日志的目录
格式标签:给日志文件套用指定的日志格式
其他语法: access_log off; #关闭access_log,即不记录访问日志 access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; 说明: buffer=size #为存放访问日志的缓冲区大小 flush=time #为缓冲区的日志刷到磁盘的时间 gzip[=level] #表示压缩级别 [if = condition] #表示其他条件
lof_format参数的标签段位置:
http
access_log参数的标签段位置:
http, server, location, if in location, limit_except 参考资料:http://nginx.org/en/docs/http/ngx_http_log_module.html
Nginx配置访问日志过程介绍
(1)创建log_format语句
vi conf/nginx.conf #vi编辑nginx主配置文件,添加标签为main的log_format格式(http标签内,在所有的server标签内可以调用)
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include status.conf;
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"';
access_log logs/access.log main;
server {
listen 80;
server_name localhost;
rewrite ^/.* http://www.abc.com permanent;
}
include vhost/*.conf;
}
(2)插入access_log语句
vi conf/vhost/www.abc.com.conf
#vi编辑虚拟主机配置文件
server {
access_log /data/log/www;
listen 80;
server_name abc.com www.abc.com;
location / {
root /data/www/www;
index index.html index.htm;
}
error_log logs/error_www.abc.com.log error;
access_log logs/access_www.abc.com.log main;
#新增内容↑
}
(3)重启服务
(4)查看访问日志文件
ll logs/access_www.abc.com.log
-rw-r--r-- 1 root root 2305 Jun 13 18:25 logs/access_www.abc.com.log
12.11 Nginx日志切割
nginx日志默认情况下统统写入到一个文件中,文件会变的越来越大,非常不方便查看分析。以日期来作为日志的切割是比较好的,通常我们是以每日来做统计的。
1、编写自动分割Nginx日志脚本
#!/bin/bash
#Rotate the Nginx logs to prevent a single logfile from consuming too much disk space.
LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
## 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
:wq保存,并命名为nginxLogRotate.sh,保存到目录/usr/local/nginx/logs
2、设置Linux定时任务
vi /etc/crontab
在打开的文件底部添加如下内容:
0 0 * * * root /usr/local/nginx/logs/nginxLogRotate.sh
:wq保存,表示配置一个定时任务,定时每天00:00以root身份执行脚本/usr/local/nginx/logs/nginxLogRotate.sh,实现定时自动分割Nginx日志(包括访问日志和错误日志) 至此,就实现了自动分割Nginx日志,Nginx每天都会生成一个新的日志文件。
12.12 静态文件不记录日志和过期时间
1.在虚拟主机配置文件添加以下配置:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配
{
expires 7d; //缓存时间
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 test.com/1.jpg 可以看到以下图没记录,当我输入jpgass就有记录了,证明生效
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 ;
if ($invalid_referer){
return 403;
}
access_log off;
}
12.14 Nginx访问控制
Nginx访问控制-目录
需求:访问/admin/目录的请求,只允许某几个IP访问:
location /admin/
{
allow 192.168.74.129;
allow 127.0.0.1;
deny all;
}
mkdir /data/wwwroot/test.com/admin/
echo “test,test”>/data/wwwroot/test.com/admin/1.html
-t && -s reload
curl -x127.0.0.1:80 test.com/admin/1.html -I
curl -x192.168.133.130:80 test.com/admin/1.html -I
除了上述简单的限制目录外,也可以根据正则匹配来限制:
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
根据user_agent限制:
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
Nginx访问控制
Nginx访问控制,在平时运维网站的时候,经常会有一些请求不正常,或者故意的做一些限制,一些重要的内容禁止别人访问,就可以做一个白名单,只允许自己的公网IP或者自己公司内的公网IP去访问
增加访问控制的代码
location /admin/
{
allow 192.168.74.129; //白名单
allow 127.0.0.1; //白名单
deny all; //全部deny
}
最后结果如下
vim /usr/local/nginx/conf/vhost/test.com.conf
假设访问的目录是admin,做一个限制
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;
}
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
# {
# expires 7d;
# access_log off;
#}
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 ;
if ($invalid_referer) {
return 403;
}
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
location /admin/
{
allow 192.168.74.129;
allow 127.0.0.1;
deny all;
}
access_log /tmp/test.com.log combined_realip;
}
然后保存退出
然后检查配置文件语法错误,然后重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
测试:
curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 07 Jan 2018 21:04:13 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Wed, 03 Jan 2018 21:43:17 GMT
Connection: keep-alive
ETag: "5a4d4e75-13"
Accept-Ranges: bytes
curl -x192.168.74.150:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 07 Jan 2018 21:06:56 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Wed, 03 Jan 2018 21:43:17 GMT
Connection: keep-alive
ETag: "5a4d4e75-13"
Accept-Ranges: bytes
12.15 Nginx解析php相关配置
1.修改配置文件:
# vim /usr/local/nginx/conf/vhost/test.com.conf
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
如果是用的sock那么一定要放开php配置中的listen.mode=666(sock的权限位一定要有写的权限)
unix:/tmp/php-fcgi.sock这里的sock文件是php-fpm.conf中定义的
cat /usr/local/php-fpm/etc/php-fpm.conf配置文件中写什么就定义什么
如果php监听的是ip和端口,nginx中的配置文件就要改成
fastcgi_pass 127.0.0.1:9000;
fastcgi_param 中的路径也需要跟上面对应起来
2.测试:
vi /data/wwwroot/test.com/3.php
curl -x127.0.0.1:80 test.com/3.php
<?php
phpinfo();
重新加载
/usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
12.16 Nginx代理
正向代理的概念
正向代理,也就是传说中的代理,他的工作原理就像一个跳板, 简单的说, 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 这个代理服务器呢,他能访问那个我不能访问的网站 于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容 代理服务器去取回来,然后返回给我
从网站的角度,只在代理服务器来取内容的时候有一次记录 有时候并不知道是用户的请求,也隐藏了用户的资料,这取决于代理告不告诉网站
结论就是 正向代理 是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。
反向代理的概念
继续举例: 例用户访问 http://ooxx.me/readme 但ooxx.me上并不存在readme页面 他是偷偷从另外一台服务器上取回来,然后作为自己的内容吐给用户
但用户并不知情 这很正常,用户一般都很笨
这里所提到的 ooxx.me 这个域名对应的服务器就设置了反向代理功能
结论就是 反向代理正好相反,对于客户端而言它就像是原始服务器,并且客户端不需要进行任何特别的设置。客户端向反向代理 的命名空间(name-space)中的内容发送普通请求,接着反向代理将判断向何处(原始服务器)转交请求,并将获得的内容返回给客户端,就像这些内容 原本就是它自己的一样。
两者区别
从用途 上来讲:
- 正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径,正向代理还可以使用缓冲特性减少网络使用率
- 反向代理的典型用途是将 防火墙后面的服务器提供给Internet用户访问。反向代理还可以为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。
- 反向代理还可以启用高级URL策略和管理技术,从而使处于不同web服务器系统的web页面同时存在于同一个URL空间下。
从安全性 来讲:
- 正向代理允许客户端通过它访问任意网站并且隐藏客户端自身,因此你必须采取安全措施以确保仅为经过授权的客户端提供服务。
- 反向代理对外都是透明的,访问者并不知道自己访问的是一个代理。
配置:
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;
}
}
Proxy_pass指定要代理的域名所在的服务器IP。
https://coding.net/u/aminglinux/p/nginx/git/blob/master/proxy/f_proxy.mdhttps://coding.net/u/aminglinux/p/nginx/git/tree/master/3z nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943 502问题汇总 http://ask.apelearn.com/question/9109 location优先级http://blog.lishiming.net/?p=100