nginx 网站配置、虚拟主机

一、默认网站

当 Nginx 配置文件中有且只有一个 Server 的时候,该 Server 就被 Nginx 认为是默认网站,所有发给 Nginx 服务器 80 端口的数据都会默认给该 Server.

[root@ip-172-31-12-127 conf]# egrep  -v "^$|#" nginx.conf
user  www;
worker_processes  2;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    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;
    sendfile        on;
    keepalive_timeout  5;
    # 默认网站
    server {
        listen       80;
        server_name  localhost;
        # 网站根目录,nginx 的安装目录:/usr/local/nginx/
        # root  /usr/local/nginx/html
        location / {
            root   html;
            index  index.html index.htm;
        }

        # 定义 404 错误访问 404.html 页面
        error_page  404              /404.html;
        # 自己定义 404 页面位置;/usr/local/nginx/html/404.html
        location = /404.html {
            root html;
        }

        # 定义 500 502 503 504 错误访问 50x.html 页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

# 404 页面内容
[root@ip-172-31-12-127 centos]# cat /usr/local/nginx/html/404.html
404

二、访问控制

location /a {
    autoindex on;
    allow 192.168.12.0/24;
    deny all;
    #基于客户端IP做过滤,符合条件的允许访问,不符合的返回404;
    if ( $remote_addr !~ "192.168.12" ) {
          return 404;
          # return http:// book.ayitula.com;
    }
}

三、登陆验证

  • auth basic

    语法:auth_basic string off;

    默认值:auth basic off;

  • auth_basic_user_file file;

location /c {
    auth_basic "登陆验证";
    auth_basic_user_file /usr/local/nginx/htpasswd;
    # 也可以针对 网段开放;
    allow 192.168.12.0/24;
    deny all;
}

生成 htpasswd 文件

# htpasswd 该文件生成依赖 htpasswd 命令
# 首先安装 httpd-devel 插件
# -c 创建;-m md5加密;如果向该文件添加用户,使用 -m 不实用-c ;否则会覆盖该文件
[root@ip-172-31-12-127 centos]# htpasswd -cm /usr/local/nginx/htpasswd test
New password:
Re-type new password:
Adding password for user test

[root@ip-172-31-12-127 centos]# cat /usr/local/nginx/htpasswd
test:$apr1$T4t9CLKi$qdsoQdmkwoHPkoV93EzYm/

# 创建第二个用户
[root@ip-172-31-12-127 centos]# htpasswd -m /usr/local/nginx/htpasswd test2
New password:
Re-type new password:
Adding password for user test2

# 静默方式添加 test2 到该文件;
htpasswd -b /usr/local/nginx/htpasswd test2 jfoasijifoe

# 查看该文件;
[root@ip-172-31-12-127 centos]# cat /usr/local/nginx/htpasswd
test:$apr1$T4t9CLKi$qdsoQdmkwoHPkoV93EzYm/
test2:$apr1$8vZ/ekIa$9q8s1CJJJSVSKOkZ0D7hW.

四、防盗链

根据 referer 来判断请求来源;

location /images/ {
alias /data/images/;
valid_referers none blocked *.ayitula.com;
    if ($invalid_referer) {
    # 如果有 referer 返回一个图片;
    rewrite ^/  http://www.ayitula.com/daolian.gif;
    # 返回 403
    #return 403;
    }
}

refer 讲解:http://nginx.org/en/docs/http/ngx_http_referer_module.html

五、Nginx 状态信息 stub_status

    server {
        listen       80;
        server_name  bbb.com;
        location / {
            stub_status on;
            access_log on;
        }
    }
[root@172 conf]$ curl bbb.com
Active connections: 1
server accepts handled requests
 28 28 29
Reading: 0 Writing: 1 Waiting: 0

########
Active connections: 1 # 表示在处理的活动连接数
server 28 # 表示从nginx启动到现在处理了 28 个链接
accepts 28  # 表示nginx从启动到现在建立了 多少次握手
# 请求丢失数 = 握手次数 - 连接数;可以看出本次状态没有丢失请求
handled requests 29 # 表示总共处理了 29 次请求
Reading 0 # 为 nginx 读取到 客户端的 header 信息数为 0
Writing: 1  # 表示 nginx 返回给客户端的 header 信息数量为 1
Waiting: 0  # 表示 nginx 已经处理完正在等候下一次请求指令的驻留链接;在开启 keep-alive 的情况下,这个值等于 active-(reading + writing).

虚拟主机

一、虚拟主机介绍

虚拟主机 就是把一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录,可以独立发布一个网站。

实验案例: 同时发布两个网站:

  • DocumentRoot /usr/local/nginx/html/web1
  • DocumentRoot /usr/local/nginx/html/web2

二、基于IP的虚拟主机

应用场景:IP充足的环境,每个网站需要一个IP地址

    server {
        listen       192.168.11.251:80;
        location / {
            root   html/web1;
            index  index.html index.htm index.php;
        }
    }
    server {
        listen       192.168.11.252:80;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }

基于IP的虚拟主机特点

  • 不同IP对应不同网站
  • 访问方便,用户直接使用默认端口即可访问
  • 服务器需要有多个IP地址(一个公网IP大概一年的费用是600左右)
  • 维护方便,基于独立IP的站点,便于监控、维护。

三、基于端口的虚拟主机

应用场景:IP不足的环境

  • 优点: 多个网站发布使用该配置方法只需要一个IP,节省IP地址
  • 缺点 端口你是无法告诉公网用户,无法适用于公网客户,适合内部用户
基于端口
server {
    listen       80;
    #server_name  www.zutuanxue.com;
    location / {
        root   html/web1;
        index  index.html index.htm index.php;
    }
}
server {
    listen       8080;
    #server_name  www.zutuanxue.com;
    location / {
        root   html/web2;
        index  index.html index.htm;
    }
}

基于端口的虚拟主机特点

  • 不同端口对应不同网站
  • 访问需要加端口
  • 节省IP地址
  • 适合私网运行

四、基于域名的虚拟主机

应用场景:一个网站需要有一个域名,目前公网发布网站的首选

基于域名
server {
    listen       80;
    server_name  web1.zutuanxue.com;

    location / {
        root   html/web1;
        index  index.html index.htm index.php;

    }
}


server {
    listen       80;
    server_name  web2.zutuanxue.com;

    location / {
        root   html/web2;
        index  index.html index.htm;
    }
}

基于域名的虚拟主机特点

  • 不同域名对应不同网站
  • 需要多个域名 可以是二级或三级域名
  • 每个站点使用默认端口,方便用户访问
  • 只需要一个IP地址,节约成本
  • 适合公网环境