怎么用虛擬目錄和創(chuàng)建站點(diǎn)的方法分別在同一臺服務(wù)器上配置兩個(gè)不同的網(wǎng)站?
時(shí)間:2024-02-01 03:30:02 | 來源:網(wǎng)站運(yùn)營
時(shí)間:2024-02-01 03:30:02 來源:網(wǎng)站運(yùn)營
怎么用虛擬目錄和創(chuàng)建站點(diǎn)的方法分別在同一臺服務(wù)器上配置兩個(gè)不同的網(wǎng)站?:題主你這個(gè)問題其實(shí)取決于你當(dāng)前使用的Web服務(wù)器是什么,具體的方法也不同,以目前最常用的Web服務(wù)器Nginx為例配置多個(gè)站點(diǎn)方法如下:
服務(wù)器地址:192.168.1.231
域名:test1.com 目錄:/www/test1.com
域名:test2.com 目錄:/www/test2.com
該配置思路
把2個(gè)站點(diǎn) test1.com, test2.com 放到 nginx 可以訪問的目錄 /www/
給每個(gè)站點(diǎn)分別創(chuàng)建一個(gè) nginx 配置文件 test1.com.conf,test2.com.conf, 并把配置文件放到 /etc/nginx/vhosts/
然后在 /etc/nginx.conf 里面加一句 include 把步驟2創(chuàng)建的配置文件全部包含進(jìn)來(用 * 號)
重啟 nginx
實(shí)際操作:
[root@localhost ~]# mkdir /www/
http://test1.com[root@localhost ~]# mkdir /www/
http://test2.com[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# mkdir vhosts
[root@localhost nginx]# cd vhosts/
[root@localhost vhosts]# vi test1.com.conf
#增加以下內(nèi)容
server {
listen 80;
server_name test1.com www.test1.com;
access_log /www/access_test1.log main;
location / {
root /www/test1.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /www/
http://test1.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
[root@localhost vhosts]# vi test2.com.conf
#增加以下內(nèi)容
server {
listen 80;
server_name test2.com www.test2.com;
access_log /www/access_test2.log main;
location / {
root /www/test2.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /www/
http://test2.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
修改nginx.conf
備份配置文件
[root@localhost ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf20160425
[root@localhost ~]# vi /etc/nginx/nginx.conf.
#修改成以下內(nèi)容
user nginx;
worker_processes 1;
# main server error log
error_log /var/log/nginx/error.log ;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
# main server config
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"';
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name _;
access_log /var/log/nginx/access.log main;
server_name_in_redirect off;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
# 這一行是加載上面的配置文件
include /etc/nginx/vhosts/*;
}
重起nginx服務(wù)
[root@localhost ~]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@localhost ~]#
下面我們進(jìn)行測試是否成功
將nginx默認(rèn)頁面/usr/html/index.html 分別拷備到/www/
http://test1.com和/www/
http://test2.com里面
然后將index.html里面的內(nèi)容分別改成test1.com和test2.com
測試機(jī)為windowns
修改host文件
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.1.231 www.test1.com
192.168.1.231 www.test2.com
在該服務(wù)器上分別打開www.test1.com
test1.com
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx news.
Commercial support is available at NGINX | High Performance Load Balancer, Web Server, & Reverse Proxy.
Thank you for using nginx.
在該服務(wù)器上分別打開www.test2.com
test2.com
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at NGINX | High Performance Load Balancer, Web Server, & Reverse Proxy.
Thank you for using nginx.
測試成功?。。。?!
估計(jì)題主沒有看懂,所以我建議你“專業(yè)的事兒還是找專業(yè)的人來做”!聯(lián)系你服務(wù)器的管理員幫助你搞定吧!
關(guān)鍵詞:服務(wù),配置,同一,分別,目錄,虛擬,創(chuàng)建,方法