nginx基础配置

一、默认网站

验证修改后的配置文件是否有问题:/usr/local/nginx/sbin/nginx -g ../conf/nginx.conf
# 当nginx配置文件中有且只有一个server的时候,该server就被nginx认为是默认网站
    # 所有发给80端口的数据就会交给该server
    # 有多个server才称为虚拟主机
    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        # 不设定使用全局的,每个server有自己的access_log
        #access_log  logs/host.access.log  main;

        # 根目录下有两个HTML:ls nginx/html/ -> 50x.html  index.html
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

二、nginx目录访问控制

    #根目录下的目录a:例如nginx/html/a
    # 配置文件中的location是相对于根目录
    #/usr/local/nginx/html
    location /a {
        allow 127.0.0.1;  #只允许本机访问a目录下的文件
        deny all;
    }

三、nginx登录验证

# 目录用户验证:b目录下的资源需要用户密码才能访问
    location /b {
        auth_basic "登录验证";
        auth_basic_user_file /etc/nginx/htpasswd;   # 保存的查看b目录的账号密码
    }
/etc/nginx/htpasswd:保存用户名和密码,格式为“用户名:密码”,密码必须加密否则无法通过验证
使用命令:
1.安装htpasswd命令:yum -y install httpd-tools
2.创建文件(创建文件时要指定创建一个用户):htpasswd -c /etc/nginx/htpasswd alex
3.创建用户sky并输入密码:htpasswd -m /etc/nginx/htpasswd sky

四、日志管理

nginx访问日志主要有两个参数控制
    1)log_format  #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
        log_format log_name string
    2)access_log  #用来指定日志文件的路径及使用的何种日志格式记录日志
        access_log  logs/access.log  main;
    #步骤:先定义一个日志格式,再定义一个日志文件才用这个格式,
    #例如:定义一个日志“haha”并应用
    log_format haha '[$time_local] $remote_addr "$request" $status'
    access_log logs/host.access.log haha
    #也可以把日志定义为json格式:
    log_format main_json  '{'
    '"@timestamp": "$time_local",'
    '"client_ip": "$remote_addr",'
    '"request": "$request",'
    '"status": "$status",'
    '"bytes": "$body_bytes_sent",'
    '"x_forwarded": "$http_x_forwarded_for",'
    '"referer": "$http_referer",'
    '}';
    access_log  logs/access_json.log   main_json;  #应用
    ----------------
    log_format格式变量:
        $remote_addr:用于记录访问网站的客户端的ip地址;
        $remote_user:用来记录客户端用户名称;
        $time_local:用来记录访问时间与时区;
        $request:用来记录请求的url与http协议;
        $status: 用来记录请求返回的状态码;成功是200
        $body_bytes_sent:服务器发送给客户端的响应body字节数
        $http_referer:用来记录从哪个页面链接访问过来的,可以根据该参数进行防盗链设置;
        $http_user_agent:记录客户浏览器的相关信息;
        $http_x_forwarded_for:当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置

 

五、防盗链

什么是盗链?
如何使用请求头中的Referer来防盗链?

192.168.1.9作为“源图片服务器”:
访问http://192.168.1.9/a/xxx.png出现图片
192.168.1.10作为盗链机器:
访问http://192.168.1.10返回盗链 <a href="http://192.168.1.9/a/xxx.png">盗链</a>
# 例如要访问c文件夹中的图片(c里存的图片)
# 允许:none表示用户  blocked表示防火墙  *.self.com表示自己或授权第三方
# 拒绝:非上面三种的访问,返回403
location /c {
#location ~* \.(png|gif|bmp)$ {  # 这个表示网站里所有图片都不能访问
    valid_referers none blocked *.self.com;
    if ($invalid_referer){
        return 403;
    }
}

 

六、nginx虚拟主机

虚拟主机介绍
一个web服务器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、域名)
使用一个web服务器软件发布多个网站
虚拟主机:把一台物理服务器划分为多个虚拟服务器,每一个虚拟主机都可以有独立的域名和独立的目录
  • 基于IP的虚拟主机
条件:
  1)两个IP:192.168.10.42:80;192.168.10.52:80;使用逻辑网卡
  2)DR存在
  DocumentRoot /usr/local/nginx/html/web1
  DocumentRoot /usr/local/nginx/html/web2
  3)索引页 index.html
添加逻辑网卡:ifconfig ens33:1 192.168.10.22 up
ens33:本地网卡名称,通过ifconfig查看
192.168.10.22:逻辑网卡地址
缺点:
  # 每个网站都需要一个IP
  # 缺点 需要多个IP 如果是公网IP 每个IP都需要付费
配置文件:
server {
    listen 192.168.10.42:80;
    location / {
        root html/web1;
        index index.html index.htm index.php;
    }
}
server {
    listen 192.168.10.52:80;
    location / {
        root html/web2;
        index index.html index.htm index.php;
    }
}

 

  • 基于端口的虚拟主机
缺点:
  # 只需要一个IP
  # 缺点 端口你是无法告诉公网用户 无法适用于公网用户,适合于内部用户
server {
    listen 80;
    location / {
        root html/web1;
        index index.html index.htm index.php;
    }
}
server {
    listen 8080;
    location / {
        root html/web2;
        index index.html index.htm index.php;
    }
}

 

  • 基于域名的虚拟主机
# 一个网站必然有一个域名
# 公网域名是在dns服务器上解析的,做实验的时候只能在  /etc/hosts 里做强制解析
server {
    listen 80;
    server_name www.abc.com;
    location / {
        root html/web1;
        index index.html index.htm index.php;
    }
}
server {
    listen 80;
    server_name www.cbd.com;
    location / {
        root html/web2;
        index index.html index.htm index.php;
    }
}

 

24234









posted on 2019-09-10 10:33  要一直走下去 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/staff/p/11496021.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值