- 配置虛擬主機
就是在一台服務器啟動多個網站。
如何區分不同的網站:
1、域名不同
2、端口不同
1.1. 通過端口區分不同虛擬機
Nginx的配置文件:
/usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}
可以配置多個server,配置了多個虛擬主機。
添加虛擬主機:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
重新加載配置文件
[root@localhost nginx]# sbin/nginx -s reload
1.2. 通過域名區分虛擬主機
1.2.1. 什么是域名
域名就是網站。
Tcp/ip
Dns服務器:把域名解析為ip地址。保存的就是域名和ip的映射關系。
一級域名:
二級域名:
三級域名:
一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。
本地測試可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射關系,如果hosts文件中配置了域名和ip的對應關系,不需要走dns服務器。
1.2.2. Nginx的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.taobao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-taobao;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-baidu;
index index.html index.htm;
}
}
域名的配置:
192.168.25.148 www.taobao.com
192.168.25.148 www.baidu.com
- 反向代理
反向代理服務器決定哪台服務器提供服務。
返回代理服務器不提供服務器。也是請求的轉發。
2.2. Nginx實現反向代理
兩個域名指向同一台nginx服務器,用戶訪問不同的域名顯示不同的網頁內容。
兩個域名是www.sian.com.cn和www.sohu.com
nginx服務器使用虛擬機192.168.101.3
第一步:安裝兩個tomcat,分別運行在8080和8081端口。
第二步:啟動兩個tomcat。
第三步:反向代理服務器的配置
upstream tomcat1 {
server 192.168.25.148:8080;
}
server {
listen 80;
server_name www.sina.com.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat1;
index index.html index.htm;
}
}
upstream tomcat2 {
server 192.168.25.148:8081;
}
server {
listen 80;
server_name www.sohu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat2;
index index.html index.htm;
}
}
第四步:nginx重新加載配置文件
第五步:配置域名
在hosts文件中添加域名和ip的映射關系
192.168.25.148 www.sina.com.cn
192.168.25.148 www.sohu.com
查看tomcat日期: tail -f tomcat/logs/catalina.out
- 負載均衡
如果一個服務由多條服務器提供,需要把負載分配到不同的服務器處理,需要負載均衡。
upstream tomcat2 {
server 192.168.25.148:8081;
server 192.168.25.148:8082;
}
可以根據服務器的實際情況調整服務器權重。權重越高分配的請求越多,權重越低,請求越少。默認是都是1
upstream tomcat2 {
server 192.168.25.148:8081;
server 192.168.25.148:8082 weight=2;
}
</div>
原文地址:https://blog.csdn.net/weixin_43087634/article/details/84800148