目录

 

一、Nginx默认虚拟主机

二、Nginx用户认证

三、Nginx域名重定向

四、Nginx访问日志

五、Nginx日志切割

六、nginx静态文件不记录日志和过期时间

七、Nginx防盗链

八、Nginx访问控制

九、Nginx解析php相关配置

十、Nginx代理

扩展 


一、Nginx默认虚拟主机

默认虚拟主机一般是配置文件中的第一个虚拟主机。也可以通过在虚拟主机配置文件中设置default_server,指定为默认虚拟主机。

默认配置文件中只有一个虚拟主机,所以默认的虚拟主机就是该虚拟主机。

修改nginx配置文件:vim /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;              定义虚拟主机配置文件位置

}

在niginx安装目录conf下创建vhost子目录:mkidr /usr/local/nginx/conf/vhost
在vhost目录下创建虚拟主机配置文件:vim /usr/local/nginx/conf/vhost/aaa.com.conf     增加内容如下
server

{

    listen 80 default_server;    有default_server标记的就是默认虚拟主机

    server_name aaa.com;       可以定义多个域名

    index index.html index.htm index.php;

    root /data/wwwroot/default;

}

测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
创建default目录,并创建index.php文件,测试如图:


LNMP架构 【2】

LNMP架构 【2】

 

 

二、Nginx用户认证

创建一个test.com的虚拟主机: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;       定义用户名密码文件

    }

}


使用命令htpasswd(apache的密码创建工具,没有安装Apache时直接使用yum安装即可):/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd zhaoyujie      再次创建密码时不需要-c选项。不然会重置密码文件。
测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl测试不输入用户名和密码时,报错状态码401。

LNMP架构 【2】

使用curl测试输入用户和密码,访问正确。但没有创建test的目录所以显示状态码404。

LNMP架构 【2】
增加一个test目录,并创建index.html文件,访问正确,状态码200

LNMP架构 【2】
在虚拟主机配置文件中修改location处的配置,也可以指定具体的目录需要用户认证。

LNMP架构 【2】
使用~匹配某个文件需要用户认证

LNMP架构 【2】

 

 

 

三、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/wwwroot/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测试,状态码为301。

LNMP架构 【2】

 

 

四、Nginx访问日志

在nginx配置文件中可以定义日志格式: vim /usr/local/nginx/conf/nginx.conf    搜索log_format

LNMP架构 【2】
其中combined_realip即日志名称,可以自定义。
常见日志字段说明
LNMP架构 【2】
虚拟主机配置文件中定义日志路径,以及引用的日志名称
设置test.com的日志路径,并引用日志格式zhaoyujie(在nginx主配置文件中需要先定义),如图:

LNMP架构 【2】
测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
查看日志文件:

LNMP架构 【2】

 

 

五、Nginx日志切割

nginx产生的访问日志文件一直就是一个,不会自动进行切割,如果访问量很大的话,将会导致日志文件容易非常大,不便于管理。可以使用shell脚本结合crontab命令非常方便的进行切割。

自定义shell脚本:vim /usr/local/sbin/nginx_logrotate.sh         内容如下
#! /bin/bash

d=`date -d "-1 day" +%Y%m%d`                      生成前一天的日期,格式为年月日

logdir="/data/logs"                                            定义日志路径          

nginx_pid="/usr/local/nginx/logs/nginx.pid"  定义PID路径

cd $logdir                                                           移动到日志目录下

for log in `ls *.log`                        

do

    mv $log $log-$d                                             以前一天的年月日命名,日志文件

done

/bin/kill -HUP `cat $nginx_pid`                         与-s reload类似,重新加载配置文件

使用命令sh运行脚本,选项-x显示运行过程:

LNMP架构 【2】
加入任务计划,每天0点0分执行脚本:crontab -e

LNMP架构 【2】

 


六、nginx静态文件不记录日志和过期时间

修改虚拟主机配置文件,加入以下内容
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$         定义匹配的格式

    {

          expires      7d;                 过期时间为7天

          access_log off;

    }

location ~ .*\.(js|css)$

    {

          expires      12h;               过期时间即1天

          access_log off;

    }

在test.com的虚拟主机配置文件中增加配置如下:

LNMP架构 【2】
测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl测试,访问配置文件定义的文件格式时,日志文件不记录这些操作

LNMP架构 【2】
使用curl查看过期时间,如图:

LNMP架构 【2】

 

 

七、Nginx防盗链

以test.com为例,修改该虚拟主机配置文件,增加以下内容:
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;                                                                         

}

测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl,以及选项-e模拟referer为http://www.baidu.com时,报错状态403

LNMP架构 【2】
使用curl,以及选项-e模拟referer为test.com时,访问正确。

LNMP架构 【2】

 

 

八、Nginx访问控制

nginx可以针对目录的访问进行控制

限制test.com/adim的请求,只允许某几个IP访问,配置如下:vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/

{

    allow 192.168.157.128;             只要这个IP在前面匹配规则生效后,不会再去匹配后面的规则

    allow 127.0.0.1;

    deny all;

}

测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl测试,IP192.168.157.128和127.0.0.1时访问正确

LNMP架构 【2】

使用curl测试其他IP时,无法访问状态码403

LNMP架构 【2】
禁止解析PHP,配置如下
location ~ .*(abc|image)/.*\.php$             匹配包含有abc或image结尾为php的文件

{

        deny all;                                             全部拒绝

}

重新加载配置文件后,测试如下

LNMP架构 【2】
根据user_agent限制访问,配置如下:

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')             使用*可以不区分大小写。

{

      return 403;

}

其中deny all和return 403效果一样

重新加载配置文件后,测试结果如下,匹配到TOmato访问拒绝,状态码403.

LNMP架构 【2】

 

 

九、Nginx解析php相关配置

新配置的虚拟主机还没有办法进行虚拟主机的解析。需要对配置文件进行修改。

修改虚拟主机配置文件,以test.com为例增加以下内容:
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;

    }

测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl测试,即可解析PHP文件
直接监听IP和端口时配置方式:fastcgi_pass 127.0.0.1:9000
php解析配置常见问题(502)
.fastcgi_pass参数配置错误,如将unix:/tmp/php-fcgi.sock配置成unix:/tmp/php-cgi.sock
fastcgi_pass中配置的参数与/usr/local/php-fpm/etc/php-fpm.conf中配置的listen不一致
若监听方式为套接字方式(php-fcgi.sock)如果php-fpm.conf中的listen.mode权限配置错误,也会提示错误
有时候服务器资源不足也可能会导致502错误。需要对程序进行优化或升级配置。
 

 

十、Nginx代理

LNMP架构 【2】

用户无法访问服务器,或者链接不通畅时,此时可以创建代理服务器解决这些问题
在目录/usr/local/nginx/conf/vhost下创建新的配置文件:vim proxy.conf
server

{

    listen 80;

    server_name www.baidu.com;             需要代理的网站


    location /

    {

        proxy_pass      http://115.239.211.112/;        代理网站的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;

    }

}
测试配置文件是否正确:/usr/local/nginx/sbin/nginx -t
重新加载配置文件:/usr/local/nginx/sbin/nginx -s reload
使用curl测试,可以通过本地IP访问到www.baidu.com

LNMP架构 【2】

 

 

扩展 

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

相关文章: