zoukankan      html  css  js  c++  java
  • Linux-12 nginx多虚拟主机

    引子:

    假设我想创业,准备2个网站,展示不同的电影,就要用到nginx多虚拟主机,在一台机器上,安装一个nginx,运行多个虚拟主机,不同的虚拟主机,返回不同的页面
    

    1.多虚拟主机的配置
    1.修改nginx底下的conf/nginx.conf ,修改信息如下

    #这里可以用vim 命令 yy 复制 和 p 粘贴例:要复制9行  9yy 复制9行 ,用 p  粘贴yy复制的内容

    server { listen 80; server_name www.bojie.com; #这里是nginx通过这个参数,来定位不同的虚拟主机 location / { #指明网页根目录在/opt/html/文件夹下 root /data/bojie; index index.html index.htm; } } server { listen 80; server_name www.cj.com; #这里是nginx通过这个参数,来定位不同的虚拟主机 location / { #指明网页根目录在/opt/html/文件夹下 root /data/cj; index index.html index.htm; } }

    2.重新加载nginx配置文件

    ../sbin/nginx -t  #检测语法
    ../sbin/nginx -s reload #平滑重启
    

    3.准备不同的虚拟主机的站点资料

    mkdir -p /data/{bojie,cj}
    放入不同的资料如下
    ├── bojie
    │   ├── bojie.jpg
    │   └── index.html
    └── cj
        ├── index.html
        └── cj.jpg
    

    4.解决本地无域名情况:写入到本地dns解析文件,由于我是在windows中通过浏览器访问,应该在windows的hosts文件中添加记录

    hosts文件就是一个本地dns(就是将域名转化成ip地址)强制解析的文件
    windows的hosts文件就在这里:C:WindowsSystem32driversetchosts ,写入如下信息
    
    192.168.0.102   www.bojie.com
    192.168.0.102  www.cj.com
    

    5.在windows中,测试访问多域名配置

    2.nginx状态模块功能

    1.在配置文件中,添加一个参数即可
            location /status {
            stub_status on;
    }
    
    2.平滑重启nginx
    ./sbin/nginx -s reload
    
    3.访问status页面
    
    http://192.168.0.102/status
    
    通过ab压测命令检测(需要安装相应模块)

       -n requests #执行的请求数,即一共发起多少请求。

       -c concurrency #请求并发数。

       -#启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。

       ab -kc 1000 -n 100000 http://192.168.0.102/

     

    3.nginx访问日志功能:

    1.在配置文件中,打开注释,nginx.conf 
    
    
       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;
    	
    参数详解:
    $remote_addr    记录客户端ip
    $remote_user    远程用户,没有就是 “-”
    $time_local    对应[14/Aug/2018:18:46:52 +0800]
    $request     对应请求信息"GET /favicon.ico HTTP/1.1"
    $status      状态码
    $body_bytes_sent  571字节 请求体的大小
    $http_referer  对应“-”  由于是直接输入浏览器就是 -
    $http_user_agent  客户端身份信息
    $http_x_forwarded_for  记录客户端的来源真实ip 97.64.34.118
    
    
    打开了功能之后,可一通过access_log查看用户身份信息
    192.168.12.60 - - [06/Dec/2018:11:24:04 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
    

    4.nginx的错误页面优化:

    1.在配置文件中添加  nginx.conf 
    	server {
    			listen       80;
    			server_name  www.s14huoying.com;
    			location / {
    				#指明网页根目录在/opt/html/文件夹下
    				root   /data/huoying;
    				index  index.html index.htm;
    				deny 192.168.12.120;
    			}
    			error_page  400 401 402 404   /40x.html;
    			#当请求来自于wwww.s14huoying.com/status/
    	}
    vim 40x.html <img style='100%;height:100%;' src=https://pic1.zhimg.com/80/v2-77a9281a2bebc7a2ea5e02577af266a8_hd.png>

      

    5.提示:如果遇到这个错误E325: ATTENTION Found a swap file by the name ".nginx.conf.swp"

    手动删除暂存文件: 
    因为 .swp 暂存文件是隐藏文件,所以需要使用 ls -a 命令显示出所有文件,然后使用 rm -f .nginx.conf.swp 命令进行删除。
    

      

     

    幻想毫无价值,计划渺如尘埃,目标不可能达到。这一切的一切毫无意义——除非我们付诸行动。
  • 相关阅读:
    prism.js——让网页中的代码更好看
    WebAPI之FormData
    ES6背记手册
    搭建本地的百度脑图
    webpack到底是干什么用的?
    浅拷贝和深拷贝
    vue 中使用 watch 的各种问题
    跳一跳外挂的python实现--OpenCV步步精深
    Opencv基础课必须掌握:滑动条做调色盘 -OpenCV步步精深
    Opencv稍微高级点的鼠标事件-OpenCV步步精深
  • 原文地址:https://www.cnblogs.com/TodayWind/p/13716554.html
Copyright © 2011-2022 走看看