Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)
1.安装nginx
# pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能
# 的Rewrite模块,rewrite可以实现动态页面转成伪静态、301网页跳转等功能.
yum -y install pcre pcre-devel openssl openssl-devel gcc gcc+
useradd www -M -s /sbin/nologin
useradd oldboy
mkdir /home/oldboy/tools/
cd /home/oldboy/tools/
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar xf nginx-1.8.0.tar.gz
# 将这句注释掉取消Debug编译模式 大概在179行
sed -i "179s/#//" auto/cc/gcc
./configure --prefix=/opt/nginx --user=www --group=www \
--with-http_stub_status_module --with-http_ssl_module
make
make install
mkdir /application
ln -s /opt/nginx/ /application/nginx
# 开发用的目录地址是/application/nginx,好处是如果nginx要升级,就可以升完级,
# 然后删除软链接,再软链一个一样的目录名--/application/nginx
curl -I 10.0.0.8 HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 11 Dec 2018 18:01:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Dec 2018 17:38:35 GMT
Connection: keep-alive
ETag: "5c0ff61b-264"
Accept-Ranges: bytes # 排错三部曲--ping、telnet、wget(或curl -I)
2.配置
# 把空行、注释行都去掉
egrep -v "^$|#" nginx.conf.default >nginx.conf
配置文件中index index.html index.htm;的意思是:访问URL什么都不跟时,会从index后面的文件挨个查找.
root html;指站点的根目录是html,相对于安装目录来说;
# 更改了软链接的内容,源文件或源目录也会相应发生变化.
# windows下命令窗口输入drivers可以迅速找到hosts文件.
nginx使用一个server{}标签来表示一个虚拟主机,一个web服务可以有多个
虚拟主机标签对,即-可以同时支持多个虚拟主机站点.
wget的三个参数:
-O:下载的文件存放到指定的文件夹下,同时重命名下载的文件;
-c:断点续传;
-P LOCAL_DIR:保存所有的文件或目录到指定的目录下.
3.访问基于域名的虚拟主机的基本流程
在浏览器输入域名,经DNS解析为IP地址之后,在请求头的host中携带着域名就去服务器端了,
服务器会根据请求头中的域名去做location匹配;
直接拿IP去访问服务器,默认是返回第一个匹配到的网页.
ping这些网站,返回的都是一个IP:www.51cto.com、blog.51cto.com、home.51cto.com、edu.51cto.com
在server中启用此参数--autoindex on,可以使此目录变成下载目录.
cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.com;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
# 直接用IP地址访问,请求头的host中没有域名,会把匹配到的第一个文件返回:
curl 10.0.0.8 -- www
4.基于端口的虚拟主机
用10.0.0.61去测试,在hosts中添加:
172.16.1.8 web01 www.etiantian.com bbs.etiantian.com worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.com;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 81;
server_name bbs.etiantian.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
此时分别去访问这两个域名:
curl www.etiantian.com -- www
curl bbs.etiantian.com -- www
此时分别去访问这两个域名:
curl www.etiantian.com -- www
curl bbs.etiantian.com -- www
都不加端口,会发现返回内容都是www,第一个是对的,第二个返回内容不是我们想要的结果,
因为以bbs.etiantian.com不加端口访问时,默认是80,经DNS解析的IP地址是172.16.1.8,
带着这样的请求头去找服务器,没有相应的匹配,就会返回匹配到的第一个html.
5.基于IP的虚拟主机
# ifconfig添加的IP叫别名IP
ifconfig eth0:1 10.0.0.101/24 up
ifconfig eth0:1 down
# ip添加的IP叫辅助IP,加了label之后,会让你看不出来是用哪个命令配的
# 一出手就是专业,高手打羽毛球不会让你预判出他发的是什么球
ip addr add 10.0.0.101/24 dev eth0 label eth0:2
ip addr del 10.0.0.101/24 dev eth0 label eth0:2 worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 10.0.0.8:80;
server_name www.etiantian.com;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 10.0.0.101:80;
server_name bbs.etiantian.com;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
curl 10.0.0.8 -- www
curl 10.0.0.101 -- bbs
6.日志及主机别名
worker_processes 1;
events {
worker_connections 1024;
}
# 可以放在main、http、server、location等位置
# 用较低级别的日志级别(如:debug|info|notice)会产生很大的磁盘I/O
error_log logs/error.log error
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout 65;
incloud extra/*.conf
}
别名作用:
1.可以实现访问两个网站www.etiantian.com、etiantian.com,其实是访问的一个;
2.比如几台web服务器的主域名相同,监控每台机器状态时,可以通过别名来准确监控.
7.nginx状态模块
cat >> /application/nginx/conf/extra/status.conf<<EOF
# status
server{
listen 80;
server_name status.etiantian.com;
location /nginx_status {
stub_status on;
access_log off;
allow 10.0.0.0/24;
deny all;
}
}
EOF
# 在10.0.0.61上访问测试
curl status.etiantian.com/nginx_status
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
# 这是因为在10.0.0.61的hosts中status.etiantian.com被解析为172.16.1.8,
# 服务器端对IP段进行了限制,能与172.16.1.8通信的是172.16.1.61,
# 所以服务器返回了403,修改hosts之后即可正常访问--10.0.0.8 status.etiantian.com
172.16.1.61 - - [15/Dec/2018:20:58:31 +0800] "GET / HTTP/1.1" 200 4 "-"
Active connections: 120
server accepts handled requests
200 200 400
Reading: 40 Writing: 20 Waiting: 60
Active connections:表示nginx正在处理的活动连接数;
server:从启动到现在共处理了200个连接;
accepts:从启动到现在共创建了200次握手;
请求丢失数=握手数-连接,可以看出,本次状态统计中没有丢失请求;
handled requests:总共处理了400次请求;
Reading:nginx读到客户端的Header信息数;
Writing:nginx返回给客户端的Header信息数;
Waiting:已经处理完正在等候下一次请求指令的驻留连接,开启 keep-alive 的情况下,这个值等于active–(reading+writing)
8.Apache基于域名的虚拟主机(IP地址10.0.0.7)
mkdir /server/tools/
cd /server/tools/
wget http://archive.apache.org/dist/httpd/httpd-2.2.32.tar.gz
tar xf httpd-2.2.32.tar.gz
cd httpd-2.2.32/
yum -y install zlib zlib-devel
./configure --prefix=/application/apache2.2.32 \
--enable-deflate --enable-expires --enable-headers \
--enable-modules=most --enable-so --enable-rewrite --with-mpm=worker
make
make install
/application/apache/htdocs
mkdir www blog bbs
# 去掉配置文件中396行的注释
vi /application/apache/conf/httpd.conf +406
Include conf/extra/httpd-languages.conf
vi /application/apache/conf/httpd.conf +98
ServerName 127.0.0.1:80
# 取消把根目录当做下载站点功能
vi httpd.conf +145
Options -Indexes FollowSymLinks cat /application/apache/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1746465163-@qq.com
DocumentRoot "/application/apache2.2.32/htdocs/www"
ServerName www.etiantian.com
ServerAlias etiantian.com
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost> <VirtualHost *:80>
ServerAdmin 1746465163-@qq.com
DocumentRoot "/application/apache2.2.32/htdocs/bbs"
ServerName bbs.etiantian.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost> <VirtualHost *:80>
ServerAdmin 1746465163-@qq.com
DocumentRoot "/application/apache2.2.32/htdocs/blog"
ServerName blog.etiantian.com
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
# 没有首页文件只是报403 Forbidden的原因之一
echo "apache blog" > blog/index.html
echo "apache bbs" > bbs/index.html
/application/apache/bin/apachectl -t
/application/apache/bin/apachectl start
/application/apache/bin/apachectl graceful # apache更改网站的根目录之后,访问报403
<VirtualHost *:80>
ServerAdmin 1746465163-@qq.com
DocumentRoot "/var/html/www"
ServerName www1.etiantian.com
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
# 这是因为配置文件中没有新增这个目录路径
<Directory "/application/apache2.2.32/htdocs">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory> <Directory "/var/html">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory> 查询第n-m条数据的sql:
select * from show_md5_status limit 50,100;
select * from show_md5_status order by id desc limit 100;
Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)的更多相关文章
- CentOS 7运维管理笔记(8)----Apache基于域名的虚拟主机配置
使用基于域名的虚拟主机配置是比较流行的方式,可以在同一个IP上配置多个域名并且都通过80端口访问. (1) 在网卡 eth0的第五个接口上配置 192.168.1.215 这个地址: (2) 配置/e ...
- Nginx总结(四)基于域名的虚拟主机配置
前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要说的 ...
- CentOS 7运维管理笔记(6)----Apache 基于 IP 的虚拟主机配置
Apache 配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置.本篇随笔记录自己基于IP的虚拟主机配置. 如果同一台服务器有多个IP,可以使用基于IP的 ...
- •搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机
本节所讲内容: 实战:搭建LAMP环境及快速部署双网站并实现基于域名的虚拟主机 LAMP架构:??? Linux+Apache+Mysql+PHP Linux+Apache+Mysql/MariaDB ...
- Apache基于域名、端口、IP的虚拟主机配置(Centos 6.5)
虚拟主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的ip,需要虚拟主机功能.一句话,一个http服务要配置多个站点,就需要虚拟主机. 虚拟主机分类:基于域名.基于端口 ...
- centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB 注意down掉网卡的方法 nginx效率没有LVS高 ipvsadm命令集 测试LVS方法 第三十三节课
centos LB负载均衡集群 三种模式区别 LVS/NAT 配置 LVS/DR 配置 LVS/DR + keepalived配置 nginx ip_hash 实现长连接 LVS是四层LB ...
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
VMware虚拟机上网络连接(network type)的三种模式--bridged.host-only.NAT VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换 ...
- [转]VMware Workstation网络连接的三种模式
经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...
- 应用负载均衡之LVS(一):基本概念和三种模式
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
随机推荐
- i2c drivers
Linux设备驱动程序架构分析之一个I2C驱动实例 转载于:http://blog.csdn.net/liuhaoyutz 内核版本:3.10.1 编写一个I2C设备驱动程序的工作可分为两部分 ...
- LeetCode(289)Game of Life
题目 According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cel ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- POJ:2777-Count Color(线段树+状压)
Count Color Time Limit: 1000MS Memory Limit: 65536K Description Chosen Problem Solving and Program d ...
- poj 3187 三角数问题
题意:给你两个数,一个n表示这个三角有多少层,一个sum表示总和 思路: 类似杨辉三角 1 1 1 1 2 1 第n行的第k个数 为 n!/k!(n-k)! 暴力枚举,因 ...
- django_orm操作
查询操作和性能优化 1.基本操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 增 models.Tb1.object ...
- POJ-3278 广度优先搜索入门
#include<stdio.h> #include<stdlib.h> struct node{ int x; int s; }s[]; int main(){ ]={}; ...
- VMware-Ubuntu入门(1)
大家都说Linux系统是让程序员用起来更有成就感的系统,我也来体验下: 对于小白鼠的我,并没有直接在电脑上重装Linux系统,而是通过VMware工具搭建Ubuntu虚拟linux环境. 首先展示下V ...
- Leetcode 462.最少移动次数使数组元素相等
最少移动次数使数组元素相等 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2, ...
- Leetcode 433.最小基因变化
最小基因变化 一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任 ...