阅读本文需要安装Nginx
一 什么是配置虚拟主机
就是在一台服务器启动多个网站
二 通过端口区分虚拟主机
复制一份静态页面
cd /usr/local/nginx
cp -r html html81
修改部分内容以示区分
vim /usr/local/nginx/html81/index.html
查看配置文件
more /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; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
可以通过配置多个server来配置多个虚拟主机
添加虚拟主机 将下面配置拷贝进去(与原有的server节点同级)
vim /usr/local/nginx/conf/nginx.conf
server { listen 81; server_name localhost; location / { root html81; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
进入sbin目录
cd /usr/local/nginx/sbin
开机状态刷新配置文件
./nginx -s reload
三 通过域名区分虚拟主机
域名: 域名就是网站
DNS服务器: 把域名解析为IP地址 保存的就是域名和IP的映射关系
注意: 一个域名对应一个IP地址 一个IP地址可以被多个域名绑定
这里我准备了2个域名
www.hb218.cn www.hdcpa.cn
在阿里云上购买的 只有3-5天的使用期(可以续费) 总共花了2个大洋
买好域名之后需要在阿里云控制台 -> 云解析DNS -> 配置2个域名指向同一台nginx服务器(IP)
这里赞一下 马爸爸的阿里云平台啥都有 挺方便的
复制一份显示www.hb218.cn的静态页面
cd /usr/local/nginx
cp -r html html-hb218
修改部分内容以示区分
vim /usr/local/nginx/html-hb218/index.html
复制一份显示www.hdcpa.cn的静态页面
cd /usr/local/nginx
cp -r html html-hdcpa
修改部分内容以示区分
vim /usr/local/nginx/html-hdcpa/index.html
查看配置文件
more /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; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
可以通过配置多个server来配置多个虚拟主机
添加虚拟主机 将下面配置拷贝进去(与原有的server节点同级)
vim /usr/local/nginx/conf/nginx.conf
server { listen 80; server_name www.hb218.cn; location / { root html-hb218; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.hdcpa.cn; location / { root html-hdcpa; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
进入sbin目录
cd /usr/local/nginx/sbin
开机状态刷新配置文件
./nginx -s reload
大功告成 浏览器分别访问www.hb218.cn www.hdcpa.cn