配置虛擬主機:
就是在一臺服務器啓動多個網站。
如何區分不同的網站:
1、域名不同
2、端口不同
通過端口區分不同虛擬機
執行命令:
cd /usr/local/nginx/
cp -r html html81
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節點就是一個虛擬主機,可以配置多個server,配置多個虛擬主機,html是nginx安裝目錄下的html目錄。
添加虛擬主機:81
#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
通過域名區分虛擬主機
一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。
本地測試可以修改hosts文件。
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
可以配置域名和ip的映射關係,如果hosts文件中配置了域名和ip的對應關係,不需要走dns服務器。
同一個端口綁定不同域名:
cd /usr/local/nginx/
cp -r html html-xxx
cp -r html html-vvv
#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.xxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-xxx;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.vvv.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-vvv;
index index.html index.htm;
}
}
}
域名的配置:
192.168.00.000 www.xxx.com
192.168.00.000 www.vvv.com
重新加載配置文件
[root@localhost nginx]# sbin/nginx -s reload
nginx的反向代理:
反向代理服務器決定哪臺服務器提供服務。
返回代理服務器不提供服務器。也是請求的轉發。
Nginx實現反向代理:
兩個域名指向同一臺nginx服務器,用戶訪問不同的域名顯示不同的網頁內容。
兩個域名是www.taobao.com.cn和www.tianmao.com
nginx服務器使用虛擬機192.168.000.00
第一步:安裝兩個tomcat,分別運行在8080和8081端口。
第二步:啓動兩個tomcat。
第三步:反向代理服務器的配置
upstream tomcat1 {
server 192.168.000.00:8080;
}
server {
listen 80;
server_name www.taobao.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.000.00:8081;
}
server {
listen 80;
server_name www.tianmao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat2;
index index.html index.htm;
}
}
重新加載配置文件
[root@localhost nginx]# sbin/nginx -s reload
第四步:配置域名
在hosts文件中添加域名和ip的映射關係
192.168.25.148 www.sina.com.cn
192.168.25.148 www.sohu.com
nginx的負載均衡:
如果一個服務由多條服務器提供,需要把負載分配到不同的服務器處理,需要負載均衡。
可以根據服務器的實際情況調整服務器權重。權重越高分配的請求越多,權重越低,請求越少。默認是都是1
upstream tomcat2 {
server 192.168.25.148:8081;
server 192.168.25.148:8082 weight=2;
}
nginx的高可用:
要實現nginx的高可用,需要實現備份機。
keepalived+nginx實現主備:這裏不做介紹了,有需要的評論。