LNMP功能配置-上
默认虚拟主机
配置方式
1:删除/usr/local/nginx/conf/nginx.conf文件中http{}配置里的server{}
然后在http{}配置的最后添加一行:include vhost/*.conf
##最后/usr/local/nginx/conf/nginx.conf文件内容如下:
[root@server-lnmp ~]# cat /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
include vhost/*.conf; ##这一行是删除server{}标签以后添加的
}
##注意nginx配置文件中,一条配置必须以分号 ; 结束,否则都视为一条配置;
2:创建虚拟主机的配置文件存放目录vhost ,和虚拟主机配置文件
[root@server-lnmp ~]# mkdir /usr/local/nginx/conf/vhost
[root@server-lnmp ~]# ls -dl /usr/local/nginx/conf/vhost/
drwxr-xr-x. 2 root root 6 Jul 4 22:45 /usr/local/nginx/conf/vhost/
##创建虚拟主机配置文件,并写入配置内容
[root@server-lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@server-lnmp ~]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com ;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
}
## 一个server{} 就是一个虚拟主机
## 可以在一个配置文件中写多个虚拟主机
## 也可以给每一个虚拟主机创建单独的配置文件
## nginx的server_name与apache不同,nginx可以直接定义多个server_name
## 当server_name后面带有default_server字段时,就代表这个虚拟主机就是默认虚拟主机
## 否则跟apache相同,第一个虚拟主机为默认虚拟主机
检查配置文件是否有语法错误,平滑重启nginx服务
##为了方便使用nginx命令,可以将/usr/local/nginx/sbin目录加入PATH环境变量
[root@server-lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile
##检查语法错误,最后显示ok和successful代表没有语法上的错误
[root@server-lnmp ~]# 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
##启动nginx服务,直接执行nginx命令即可
##平滑重启nginx服务,代表在不停止nginx服务的情况下,重新加载配置文件,重启工作线程
[root@server-lnmp ~]# nginx -s reload
.
测试访问
在/data/wwwroot/目录下创建虚拟主机的根目录一个测试页文件
[root@server-lnmp ~]# mkdir -p /data/wwwroot/www.test.com
[root@server-lnmp ~]# mkdir -p /data/wwwroot/www.default.com/
[root@server-lnmp ~]# echo '<h1>This is test page</h1>' > /data/wwwroot/www.test.com/index.html
[root@server-lnmp ~]# echo '<h1>This is defalut site</h1>' > /data/wwwroot/www.default.com/index.html
使用curl测试访问www.test.com
[root@server-lnmp ~]# curl -x127.0.0.1:80 www.test.com
<h1>This is test page</h1>
使用curl测试访问www.default.com
[root@server-lnmp ~]# curl -x10.1.1.28:80 www.default.com
<h1>This is defalut site</h1>
使用curl测试访问其他没有定义过server_name的域名
[root@server-lnmp ~]# curl -x10.1.1.28:80 www.aaa.com
<h1>This is defalut site</h1>
[root@server-lnmp ~]# curl -x10.1.1.28:80 bbb.cn
<h1>This is defalut site</h1>
Nginx用户认证
配置方式
在server_name 是www.test.com的虚拟主机配置下面添加以下的配置
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
查看添加配置后的文件内容
[root@server-lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@server-lnmp ~]# cat !$
cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com ;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
##注意auth_basic_user_file 的路径一定要填正确,否则报403错误
生成用于验证的用户和密码
这里也需要用到apache的htpasswd命令
如果没有这个命令可以yum安装httpd-tools可以了
[root@server-lnmp ~]# yum install -y httpd-tools
[root@server-lnmp ~]# htpasswd -c /usr/local/nginx/conf/htpasswd testuser
New password:
Re-type new password:
Adding password for user testuser
#注:htpasswd命令的-c选项,只有在第一次创建用户认证的密码文件时需要使用,
# 如果再次添加用户和密码时使用了-c选项,则会覆盖掉之前的所有内容
检查语法错误,重载配置文件
[root@server-lnmp ~]# 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@server-lnmp ~]# nginx -s reload
测试效果
curl 测试访问www.test.com
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com
HTTP/1.1 401 Unauthorized
#状态码为401,说明需要认证才能访问
curl 测试用户认证访问
[root@server-lnmp ~]# curl -utestuser:123456 -x10.1.1.28:80 www.test.com
<h1>This is test page</h1>
针对目录的用户认证
修改test.com.conf配置文件
[root@server-lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@server-lnmp ~]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com ;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
#注意location匹配的目录不再是根目录,而是/admin/目录
#这里的根目录指的是网站的根目录:/data/wwwroot/www.test.com/
#修改完检测语法,重载配置文件
[root@server-lnmp ~]# 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@server-lnmp ~]# nginx -s reload
测试效果
创建/data/wwwroot/www.test.com/admin目录
[root@server-lnmp ~]# mkdir /data/wwwroot/www.test.com/admin
curl测试访问www.test.com
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com
HTTP/1.1 200 OK
#状态码200 访问正常
curl测试访问www.test.com/admin
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com/admin/
HTTP/1.1 401 Unauthorized
#即使访问请求的文件并不存在也需要验证
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com/admin/fasd
HTTP/1.1 401 Unauthorized
针对文件的用户认证
修改test.com.conf配置文件
[root@server-lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@server-lnmp ~]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com ;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
location ~ ^.*/admin.html
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
##注意location
##修改完检查语法,重载配置文件
测试效果
创建测试文件
[root@server-lnmp ~]# echo '/data/wwwroot/www.test.com/admin.html' >/data/wwwroot/www.test.com/admin.html
[root@server-lnmp ~]# echo '/data/wwwroot/www.test.com/admin/admin.html' >/data/wwwroot/www.test.com/admin/admin.html
curl测试访问www.test.com/admin.html
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com/admin.html
HTTP/1.1 401 Unauthorized
curl测试访问www.test.com
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com
HTTP/1.1 200 OK
curl测试访问www.test.com/admin/admin.html
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com/admin/admin.html
HTTP/1.1 401 Unauthorized
Nginx域名重定向
配置方式
修改test.com.conf配置文件
[root@server-lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@server-lnmp ~]# cat /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com abc.com;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
if ($host != 'www.test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ ^.*/admin.html
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
配置解释
#下面的就是域名重定向的配置
if ($host != 'www.test.com' ) {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
#在nginx配置文件中可以使用if判断
#这个配置的大概意思是当 $host 不是www.test.com的时候
#执行rewrite 将域名重定向到http://www.test.com
#$host就是访问请求的域名,也就是server_name
# ^/(.*)$ 这个可以匹配出域名后面的URI地址,
# $1 就是调用前面(.*)匹配到的URI,类似sed 这样就可以进行更精确的重定向了。
#最后的 permanent 表示的是永久重定向 状态码为 301
#如果改为 redirect 表示临时重定向,状态码302
关于URI
在nginx中有几个关于uri的变量,包括$uri $request_uri $document_uri $args,
下面看一下他们的区别 :
假如访问的URL为:http://www.test.com/index.php?a=1&b=2
$uri==/index.php
$request_uri==/index.php?a=1&b=2
$document_uri==/index.php
$args==?a=1&b=2
##一般uri和document_uri是相同的
测试效果
curl测试访问test.com和abc.com
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 test.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 05 Jul 2018 11:34:22 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.test.com/
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 abc.com/jfkdsa/gfda
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 05 Jul 2018 11:34:58 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.test.com/jfkdsa/gfda
[root@server-lnmp ~]# curl -I -x10.1.1.28:80 www.test.com
HTTP/1.1 200 OK
#只有访问www.test.com不会重定向
Nginx访问日志
配置方式
Nginx默认的日志格式
[root@server-lnmp conf]# grep -A2 'log_format' nginx.conf
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
#上面grep筛选出来的内容就是是日志文件的格式,
#也可以自行调整各个变量的位置
#他们分别代表的含义如下:
log_format #定义日志格式的函数
combined_realip #定义日志格式名称,可随意设定
$remote_addr #客户端的公网IP
$http_x_forwarded_for #代理服务器的IP
$time_local #服务器本地时间
$host #访问主机名(域名,网址)
$request_uri #访问的URI地址
$status #状态码
$http_referer #referer
$http_user_agent #user_agent
除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加配置行
[root@server-lnmp conf]# cd /usr/local/nginx/conf/
[root@server-lnmp conf]# vim vhost/test.com.conf
[root@server-lnmp conf]# cat vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com abc.com;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
if ($host != 'www.test.com') {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
location ~ ^.*/admin.html
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log logs/test_access.log combined_realip;
}
配置添加完成后-t测试语法错误,重载配置文件
配置文件的内容最后一行内容就是定义虚拟主机的访问日志
access_log logs/test_access.log combined_realip;
#access_log #定义访问日志功能
#logs/test_access.log #定义访问日志文件的存放路径
#combined_realip #指定nginx.conf文件中定义的日志格式名称
测试效果
使用curl访问一次www.test.com,然后查看日志文件的内容。
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com
<h1>This is test page</h1>
[root@server-lnmp conf]# cat /usr/local/nginx/logs/www_test_com.log
10.1.1.28 - [05/Jul/2018:20:36:54 +0800] www.test.com "/" 200 "-" "curl/7.29.0"
Nginx日志切割
nginx和apache不同,nginx没有自带的日志切割工具
所以需要自己编写一个日志切割脚本,
然后配合计划任务来实现日志切割的功能。
自定义日志切割脚本
脚本内容如下,一般脚本都放在/usr/local/sbin/目录下
[root@server-lnmp conf]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@server-lnmp conf]# cat /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
## 假设nginx的日志存放路径为/usr/local/nginx/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
find -mtime +30 -type f -name "*.log" -exec rm -f {} \;
done
/bin/kill -HUP `cat $nginx_pid`
任务计划
编写完日志切割脚本后,使用crontab -e添加一条任务计划
每天0点0分执行这个日志切割脚本
[root@server-lnmp conf]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@server-lnmp conf]# crontab -l
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
静态文件:不记录日志,过期时间
配置方式
编辑虚拟主机配置文件,
在虚拟主机www.test.com的配置中添加以下内容
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
##添加完检测语法,重载配置。
配置说明:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#表示location匹配URI以gif、jpg、png、bmp、swf结尾的访问请求
#当有匹配到相关的访问请求时,
expires 7d;
#设定文件缓存到浏览器的过期时间7天。
#Nd=N天,
#Nh=N小时
#Nm=N分钟
#Ns=N秒
access_log off;
#关闭日志记录
修改配置后的test.com.conf文件的内容如下
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com abc.com;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
if ($host != 'www.test.com') {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
location ~ ^.*/admin.html
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log logs/www_test_com.log combined_realip;
}
测试效果
在/data/wwwroot/www.test.com/目录下创建两个相关的测试文件
[root@server-lnmp conf]# echo 'test.jpg' > /data/wwwroot/www.test.com/test.jpg
[root@server-lnmp conf]# echo 'test.css' > /data/wwwroot/www.test.com/test.css
curl测试访问www.test.com/test.jpg 和 www.test.com/test.css
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/test.jpg
test.jpg
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/test.jpg -I
HTTP/1.1 200 OK
(省略...)
Cache-Control: max-age=604800 #这个字段就表示缓存的过期时间
Accept-Ranges: bytes
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/test.css
test.css
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/test.css -I
HTTP/1.1 200 OK
(省略...)
Cache-Control: max-age=43200
Accept-Ranges: bytes
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/index.html
<h1>This is test page</h1>
#最后访问一次index.html
查看访问日志
[root@server-lnmp conf]# cat /usr/local/nginx/logs/www_test_com.log
10.1.1.28 - [05/Jul/2018:20:36:54 +0800] www.test.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [05/Jul/2018:21:31:25 +0800] www.test.com "/index.html" 200 "-" "curl/7.29.0"
##可以看到没有访问test.jpg和test.css的访问纪律
Nginx防盗链
配置方式
防盗链可以和静态文件结合起来配置。
需要修改前面添加的静态文件的配置、修改后test.com.conf文件的内容如下:
[root@server-lnmp conf]# cat !$
cat vhost/test.com.conf
server
{
listen 80;
server_name www.default.com default_server;
index index.html index.htm index.php;
root /data/wwwroot/www.default.com/;
}
server
{
listen 80;
server_name www.test.com test.com abc.com;
index index.html index.htm index.php;
root /data/wwwroot/www.test.com/;
if ($host != 'www.test.com') {
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
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.html
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log logs/www_test_com.log combined_realip;
}
防盗链配置解释
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}
#valid_referers 配置referer白名单
#none 代表没有referer
#blocked 代表有referer但是被防火墙或者是代理给去除了
#server_names 代表这个虚拟主机的所有server_name
#*.test.com #这个可以是正则匹配的字段, 或者指定的域名
#当访问请求不包含在白名单里面时:
#invalid_referer的值为 1 ,就会执行if语句,
#当访问请求包含在白名单里面时,
#invalid_referer的值为 0 就不会执行 if 语句
测试效果
curl直接请求访问www.test.com/test.jpg
这时referer为none 所有可以访问
[root@server-lnmp conf]# curl -I -x127.0.0.1:80 www.test.com/test.jpg
HTTP/1.1 200 OK
curl 指定referer为http://baidu.com 进行访问请求
这时referer为http://baidu.com 没在白名单里面,
所以状态码返回403,拒绝访问了
[root@server-lnmp conf]# curl -I -e "http://baidu.com" -x127.0.0.1:80 www.test.com/test.jpg
HTTP/1.1 403 Forbidden
Nginx访问控制
配置方式
需求:访问/aaa/目录的请求,只允许某几个IP访问,
在虚拟主机www.test.com的配置里添加如下内容:
location /aaa/
{
allow 127.0.0.1;
deny all;
}
配置完检查语法,重载配置
[root@server-lnmp conf]# rnginx
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@server-lnmp conf]# which rnginx
alias rnginx='/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload'
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx
配置说明:
allow 127.0.0.1; #规则,允许ip 127.0.0.1访问,这里的ip就是访问日志里的$remote_addr
deny all; #规则,拒绝所有
#也可以配置为allow all;然后deny某些ip
#匹配规则是从上往下匹配,当匹配到一个规则就不再往下匹配了
测试效果
创建测试目录和文件
[root@server-lnmp conf]# mkdir /data/wwwroot/www.test.com/aaa
[root@server-lnmp conf]# echo 'aaa test' > /data/wwwroot/www.test.com/aaa/index.html
curl 将域名www.test.com/aaa/解析到127.0.0.1:80
这时系统本地就会使用127.0.0.1这个ip向nginx服务发起访问请求,
所以remote_addr就是127.0.0.1,可以正常访问
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.test.com/aaa/index.html
aaa test
curl 将域名www.test.com/aaa/解析到10.1.1.28:80
这时系统本地就会使用10.1.1.28这个ip向nginx服务发起访问请求,
所以remote_addr就是10.1.1.28,会拒绝访问
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com/aaa/index.html
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
匹配正则
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
##这样可以拒绝所有abc目录下或者image目录下以.php结尾文件的访问请求
根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
#如此可以拒绝所有user_agent为Spider/3.0、YoudaoBot、Tomato的访问请求
#deny all和return 403效果一样
Nginx解析php相关配置
配置方式
在虚拟主机www.test.com 的配置最后面的access_log前一行添加下面的内容
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;
}
##配置添加完成先不要重载配置文件
配置解释:
location ~ \.php$ #location匹配所有document_uri以 .php 结尾的访问请求
{
include fastcgi_params;
#引用fastcgi_params常量文件
fastcgi_pass unix:/tmp/php-fcgi.sock;
#指定PHP的sock文件路径,
#如果php-fpm.conf配置listen是ip:port,这里也需要配置为相同的ip:port
#这里配置错误会出现502报错
fastcgi_index index.php;
#指定php的索引页
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
#指定PHP程序的请求路径, $ 符号前面的路径需要和虚拟主机的root路径相同
#这个路径有问题会出现404报错
}
测试效果
创建测试的php文件
[root@server-lnmp conf]# echo -e "<?php\necho 'PHP test page';" > /data/wwwroot/www.test.com/test.php
在没有重载配置的情况下,先测试访问www.test.com/test.php
结果输出的是php的源代码,表示没有解析PHP
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com/test.php
<?php
echo 'PHP test page';
重载nginx配置文件后再次访问www.test.com/test.php
输出结果为PHP test page 表示成功解析了PHP脚本
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com/test.php
PHP test page
如果SCRIPT_FILENAME填写有误就会输出File not found,
获取头部信息状态码为404
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com/test.php
File not found.
[root@server-lnmp conf]# curl -I -x10.1.1.28:80 www.test.com/test.php
HTTP/1.1 404 Not Found
如果fastcgi_pass 填写有误会出现502报错
[root@server-lnmp conf]# curl -I -x10.1.1.28:80 www.test.com/test.php
HTTP/1.1 502 Bad Gateway
配置php-fpm监听 ip:port
修改/usr/local/php-fpm/etc/php-fpm.conf配置文件:
将listen = /tmp/php-fcgi.sock 修改为 listen = 10.1.1.28:9000 ,这里的ip需要根据自己的环境来填写
[root@server-lnmp conf]# vim /usr/local/php-fpm/etc/php-fpm.conf
[root@server-lnmp conf]# grep '^listen = ' /usr/local/php-fpm/etc/php-fpm.conf
listen = 10.1.1.28:9000
再修改test.com.conf中虚拟主机www.test.com的配置
将fastcgi_pass unix:/tmp/php-fcgi.sock; 修改为fastcgi_pass 10.1.1.2:9000;
[root@server-lnmp conf]# vim vhost/test.com.conf
[root@server-lnmp conf]# grep 'fastcgi_pass' vhost/test.com.conf
fastcgi_pass 10.1.1.28:9000;
重载php-fpm和nginx配置
[root@server-lnmp conf]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@server-lnmp conf]# rnginx
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@server-lnmp conf]# which rnginx
alias rnginx='/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload'
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx
测试访问www.test.com/test.php
[root@server-lnmp conf]# curl -x10.1.1.28:80 www.test.com/test.php
PHP test page
Nginx代理
配置代理服务器之前的测试
#没配置代理前将www.baidu.com解析到127.0.0.1
#结果只是访问了默认虚拟主机
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.baidu.com
<h1>This is defalut site</h1>
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.baidu.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 05 Jul 2018 17:19:51 GMT
Content-Type: text/html
Content-Length: 30
Last-Modified: Wed, 04 Jul 2018 15:41:04 GMT
Connection: keep-alive
ETag: "5b3cea90-1e"
Accept-Ranges: bytes
配置代理服务器
这里我们先用ping命令查看一下baidu.com的ip地址
[root@server-lnmp conf]# ping www.baidu.com -c1
PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=1 ttl=54 time=32.3 ms
#可以看到百度的IP为 180.97.33.107
在/usr/local/nginx/conf/vhost/目录下添加一个配置文件 proxy.conf
并写入代理服务器的配置
[root@server-lnmp conf]# vim /usr/local/nginx/conf/vhost/proxy.conf
[root@server-lnmp conf]# cat /usr/local/nginx/conf/vhost/proxy.conf
server
{
listen 80;
server_name www.baidu.com; #这里写代理服务器的域名
location /
{
proxy_pass http://180.97.33.107/; #这里的IP写web服务的ip
proxy_set_header Host $host; #设定header信息的Host变量
proxy_set_header X-Real-IP $remote_addr; #设定header信息的remote_addr变量
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #设定header信息的X-Forwarded-For变量
}
}
#复制的最好将注释删掉
#配置完成检查语法,重载nginx配置
测试效果
再次测试访问 curl -x127.0.0.1:80 www.baidu.com
将www.baidu.com解析到本机,结果可以访问到真正的百度首页了
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html>
(省略...)
使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@server-lnmp conf]# curl -x127.0.0.1:80 www.baidu.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 05 Jul 2018 18:25:24 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f74-115"
Last-Modified: Mon, 13 Jun 2016 02:50:28 GMT
Pragma: no-cache