Nginx默认虚拟主机
1.编辑配置文件 vi nginx.conf
将server部分删除,并加入include vhost/*.conf; 如下
2.切换到/usr/local/nginx/conf目录下创建/vhost/目录,再进入vhost目录中,使用vi 创建aaa.com.conf 复制如下
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
定义默认虚拟主机时,如果vhost下有多个,谁是第一位那么就是默认虚拟主机,或者加上default_server这个标记就是默认虚拟主机
3.创建 mkdir -p /data/wwwroot/default/目录下,在default/目录下 vi index.html写入 this is default
4. 重新加载
/usr/local/nginx/sbin/nginx -t // 检查语法错误
/usr/local/nginx/sbin/nginx -s reload //重新加载
5.测试
不管什么域名,只要解析过来指向该服务器,都能访问这个站点
[root@lioo111 default]# curl localhost
this is default
[root@lioo111 default]# curl -x127.0.0.1:80 aaa.com
this is default
[root@lioo111 default]# curl -x127.0.0.1:80 111.com
this is default
Nginx用户认证
1.创建 vi /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"; //定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用户名密码文件/usr/local/nginx/conf/htpasswd;
}
}
2.创建目录和文件
mkdir /data/wwwroot/test.com
echo "123.com" > /data/wwwroot/test.com/index.html
3.生成密码文件,如果之前安装过apache,可以直接使用/usr/local/apache/bin/htpasswd, 如果没有安装过apache,那么可以使用yum安装http,然后在直接使用htpasswd来生成
[root@lioo111 default]# htpasswd -c /usr/local/nginx/conf/htpasswd lioo
New password:
Re-type new password:
Adding password for user lioo
[root@lioo111 default]# cat /usr/local/nginx/conf/htpasswd
lioo:$apr1$fpUHA4ac$x5PXnfWZ9xUFy/diweUdy0
第2次创建的时候就不要使用-c选项 会覆盖之前的账号密码
4.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
5.测试
显示401需要用户认证
[root@lioo111 default]# curl -x127.0.0.1:80 -ulioo:12345 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
输入账号密码正常访问
[root@lioo111 default]# curl -ulioo:123456 -x127.0.0.1:80 test.com
123.com
设置访问目录需要用户认证
1.修改虚拟主机配置文件
vi /usr/local/nginx/conf/vhost/test.com.conf
location /admin/ //这里修改为目录
2. 创建admin目录和index文件
mkdir /data/wwwroot/test.com/admin
echo "1111111" >/data/wwwroot/test.com/admin/index.html
3. 重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
4.测试
test目录正常访问
[root@lioo111 default]# curl -x127.0.0.1:80 test.com
123.com
访问admin/index.html显示401需要用户认证
[root@lioo111 default]# curl -x127.0.0.1:80 test.com/admin/index.html
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
设置访问url需要用户认证
1.修改虚拟主机配置文件
vi /usr/local/nginx/conf/vhost/test.com.conf
location ~ admin.php //修改为匹配admin.php文件需要认证
2.创建admin.php文件
vi /data/wwwroot/test.com/admin.php
3. 重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
4. 测试
访问admin/index.html 正常显示
[root@lioo111 default]# curl -x127.0.0.1:80 test.com/admin/index.html
1111111
访问test.com/admin.php显示401用户认证
[root@lioo111 default]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
输入账号密码 正常显示200
[root@lioo111 default]# curl -ulioo:123456 -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Mon, 17 Sep 2018 14:36:32 GMT
Content-Type: application/octet-stream
Content-Length: 23
Last-Modified: Mon, 17 Sep 2018 14:36:07 GMT
Connection: keep-alive
ETag: "5b9fbbd7-17"
Accept-Ranges: bytes
Nginx域名重定向
1. 修改配置文件 vi 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; //permanent是301 redirect是302
}
在server_name后面再加一个域名test2.com,在nginx这里是可以写多个的域名,比如test1.com、test2.com,需要访问这2个域名的时候跳转到test.com上(apache中server_name中并不支持写多个域名,只认第一个。要想跟多少可以,要使用server_alias配置段来定义。)
2.重新加载配置文
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
访问test1.com 显示301域名跳转
[root@lioo111 vhost]# curl -x127.0.0.1:80 test1.com/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
访问test2.com 显示301域名跳转
[root@lioo111 vhost]# curl -x127.0.0.1:80 test2.com/index.html/sdfsdfsdf
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
Nginx访问日志
Nginx日志的格式一般在nginx.conf里面配置, 其中combined_realip为日志格式的名字,可以自定义,后面虚拟主机配置文件也要写成自定义的名字。
1. 修改主配置文件 vim /usr/local/nginx/conf/nginx.conf //搜索log_format
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
字段 |
说明 |
$remote_addr |
记录客户端IP |
$http_x_forwarded_for |
记录客户端IP |
$time_local |
服务器本地时间 |
$host |
访问的主机名(域名) |
$request_uri |
访问的uri地址 |
$status |
状态码 |
$http_referer |
refer |
$http_user_agent |
2.修改虚拟主机配置文件
vi /usr/local/nginx/conf/vhost/test.com.conf 最后面添加一行
access_log /tmp/test.com.log combined_realip; //定义日志路径以及格式
3.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
4.测试
访问test.com/index.html/sdfsdfsdf 和 test2.com/index.html/sdfsdfsdf
[root@lioo111 vhost]# curl -x127.0.0.1:80 test.com/index.html/sdfsdfsdf
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
[root@lioo111 vhost]# curl -x127.0.0.1:80 test2.com/index.html/sdfsdfsdf
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
查看日志是否记录
[root@lioo111 vhost]# cat /tmp/test.com
127.0.0.1 - [20/Sep/2018:13:56:06 +0800] test.com "/index.html/sdfsdfsdf" 404 "-" "curl/7.29.0"
127.0.0.1 - [20/Sep/2018:13:56:13 +0800] test2.com "/index.html/sdfsdfsdf" 301 "-" "curl/7.29.0"
Nginx日志切割
Nginx 没有自带的切割工具 需要自己写脚本定义任务计划删除
1. 编辑脚本 vim /usr/local/sbin/nginx_logroate.sh //脚本放在/usr/local/sbin目录下写入:
#! /bin/bash
# 假设nginx的日志存放路径为/tmp/logs
d=`date -d "-1 day" +%Y%m%d` //生成昨天的日期
logdir="/tmp"
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`
2. 脚本测试
[root@lioo111 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh //-x选项是查看执行过程
++ date -d yesterday +%Y%m%d
+ d=20180919
+ log_dir=/tmp
++ cat /usr/local/nginx/logs/nginx.pid
+ nginx_pid=1163
+ cd /tmp
++ ls test.com.log
+ for log in '$(ls *.log)'
+ mv test.com.log test.com.log_20180919
+ touch test.com.log
+ /usr/bin/kill -USR1 1163
查看/tmp/下的test日期结尾日志是否生成
[root@lioo111 vhost]# ls /tmp/
mysql.sock systemd-private-c28b994125764b18b09d7d5d17aa85c0-chronyd.service-oCDlQk
pear systemd-private-feb0aa3f78c44e7b93233e3135b837a5-chronyd.service-i5BEvd
php_errors.log-20180919 test.com.log
php-fcgi.sock test.com.log_20180919
3. 添加到任务计划中
[root@lioo111 ~]# crontab -e //添加计划任务 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh [root@lioo111 ~]# crontab -l 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
4. 删除之前的日志的,可以使用find来执行
find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm //删除修改文件时间大于30天的
静态文件不记录日志和过期时间
静态文件访问是可以不用记录到日志中,因为静态文件太多了,会占用大量的系统资源
1.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
#不记录过期时间
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配这些文件
{
expires 7d; //过期时间
access_log off; //不记录访问日志
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdfsdaf
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/2.js
123123123
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/index.html
666.com
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/2.jsgfhgf
查看日志只记录了最后两条
[root@lioo111 test.com]# cat /tmp/test.com.log
127.0.0.1 - [20/Sep/2018:15:14:38 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Sep/2018:15:15:41 +0800] test.com "/2.jsgfhgf" 404 "-" "curl/7.29.0"
查看访问状态可以看到最大保存时间
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 07:19:50 GMT
Content-Type: application/x-javascript
Content-Length: 10
Last-Modified: Thu, 20 Sep 2018 07:10:49 GMT
Connection: keep-alive
ETag: "5ba347f9-a"
Expires: Thu, 20 Sep 2018 19:19:50 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
Nginx防盗链
防止别人盗用你网站上的资源。比如:图片、视频、歌曲、文档等
1.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
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; //这段表示不是白名单访问,那么会提示403
}
access_log off; //访问日志不记录
}
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
curl -e 配置referer(网址)
网址为www.baidu.com 显示403
[root@lioo111 test.com]# curl -e "http://www.baidu.com/" -x127.0.0.1:80 test.com/1.gif -I
HTTP/1.1 403 Forbidden
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 07:35:21 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
网址为www.test.com/ 200 可以访问
[root@lioo111 test.com]# curl -e "http://www.test.com/" -x127.0.0.1:80 test.com/1.gif -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 07:35:28 GMT
Content-Type: image/gif
Content-Length: 8
Last-Modified: Thu, 20 Sep 2018 07:10:31 GMT
Connection: keep-alive
ETag: "5ba347e7-8"
Expires: Thu, 27 Sep 2018 07:35:28 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
Nginx访问控制
1.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
location /admin/ //指定目录
{
allow 127.0.0.1;
allow 192.168.217.128;
deny all;
}
Nginx访问控制只要匹配一个规矩就会生效不在匹配别的规则 如果最后第三个就会别拒绝访问
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
127.0.0.1访问正常
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/admin/ -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 07:57:40 GMT
Content-Type: text/html
Content-Length: 8
Last-Modified: Mon, 17 Sep 2018 14:19:04 GMT
Connection: keep-alive
ETag: "5b9fb7d8-8"
Accept-Ranges: bytes
192.168.217.129访问正常
[root@lioo111 test.com]# curl -x192.168.217.129:80 test.com/admin/ -I
HTTP/1.1 200 OK
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 07:58:01 GMT
Content-Type: text/html
Content-Length: 8
Last-Modified: Mon, 17 Sep 2018 14:19:04 GMT
Connection: keep-alive
ETag: "5b9fb7d8-8"
Accept-Ranges: bytes
192.168.217.128无法访问403
[root@lioo111 test.com]# curl -x192.168.135.128:80 test.com/admin/ -I
HTTP/1.1 403 Forbidden
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 08:11:16 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
禁止upload目录解析php
1.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.创建个upload目录下的php文件
mkdir /data/wwwroot/test.com/upload
echo "11111" > /data/wwwroot/test.com/upload/1.php
echo "11111" > /data/wwwroot/test.com/upload/1.txt
4. 测试
1.php 403禁止访问
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/upload/1.php
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.4.7</center>
</body>
</html>
1.txt可以访问
[root@lioo111 test.com]# curl -x127.0.0.1:80 test.com/upload/1.txt
11111
针对user_agent访问控制
1.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
if ($http_user_agent ~* 'Spider/3.0|baidu|Tomato') //*不区分大小写
{
return 403;
}
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
curl -A "baidu.com" 403禁止访问
[root@lioo111 test.com]# curl -A "baidu.com" -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 08:34:44 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
大写也禁止访问
[root@lioo111 test.com]# curl -A "123Baidu.com" -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.4.7
Date: Thu, 20 Sep 2018 08:35:03 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
Nginx解析php相关配置
在LNMP中,PHP是以一个服务的形式存在的。首先要启动php-fpm服务,然后Nginx再和php-fpm通信。也就是说处理php脚本解析的工作是由php-fpm来完成的,Nginx只是在将用户的请求传递给php-fpm,然后将处理完的结果返回给用户
1.创建 vi /data/wwwroot/test.com/3.php 写入
<?php
phpinfo();
直接访问php文件无法解析显示源码
[root@lioo111 ~]# curl -x127.0.0.1:80 test.com/3.php
<?php
phpinfo();
2.修改虚拟主机配置文件 vi /usr/local/nginx/conf/vhost/test.com.conf 加入以下内容
#解析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;
}
3.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
再次访问就能正常解析了
[root@lioo111 ~]# curl -x127.0.0.1:80 test.com/3.php
502报错
1.fastcgi_pass unix:/tmp/php-fcgi.sock; 这里写错了导致找不到sock文件
2.fastcgi_pass中配置的参数与/usr/local/php-fpm/etc/php-fpm.conf中配置的listen不一致
php-fpm中的listen有两种配置方式
listen = /tmp/php-fcgi.sock
或
listen = 127.0.0.1:9000
如果虚拟主机配置文件的里配置与php-fpm中的配置不一致也会报错,两个配置文件中必须要用一致的监听方式。
3.监听方式为套接字方式(php-fcgi.sock)如果php-fpm.conf中的listen.mode权限配置错误,也会提示错误,必须是666以上的权限
Nginx代理
Nginx的代理功能非常实用,这也是nginx比apache更流行的原因。。如果一公司有很多台服务器,为了节约成本,不能为所用服务器分配公网IP,而如果没有公网IP的服务器要提供web服务,就可以通过代理来实现
1.创建配置文件
vi /usr/local/nginx/conf/vhost/proxy.conf 写入
server
{
listen 80;
server_name www.baidu.com; //需要代理的网站
location /
{
proxy_pass http://http://61.135.169.121/; // 代理网站的IP
proxy_set_header Host $host; // 即server_name
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.重新加载配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
3.测试
[root@lioo111 ~]# curl -x127.0.0.1:80 www.baidu.com/robots.txt
User-agent: Baiduspider
Disallow: /baidu
Disallow: /s?
Disallow: /ulink?
Disallow: /link?
Disallow: /home/news/data/
User-agent: Googlebot
Disallow: /baidu
Disallow: /s?
Disallow: /shifen/
Disallow: /homepage/
Disallow: /cpro
Disallow: /ulink?
Disallow: /link?
Disallow: /home/news/data/
User-agent: MSNBot
Disallow: /baidu
Disallow: /s?
Disallow: /shifen/