由于开发需求,项目有时候需要设置虚拟域名进行测试。下面是分别是apache和nginx的配置
一、apache
环境:wampserver2.5
1.修改host文件
C:\Windows\System32\drivers\etc\host
底部追加
127.0.0.1 test.com
2.修改apache配置文件
F:\wamp\bin\apache\apache2.4.9\conf\httpd.conf
前面#号去掉,打开vhost模块
LoadModule vhost_alias_module modules/mod_vhost_alias.so
去掉#,加载虚拟配置文件,并编辑
Include conf/extra/httpd-vhosts.conf
这文件清空吧。一堆注释没什么用
#让localhost可以访问
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "F:/wamp/www" ServerName localhost ServerAlias localhost ErrorLog "logs/local-error.log" CustomLog "logs/local-access.log" common </VirtualHost> #测试 <VirtualHost *:80> DocumentRoot "F:/wamp/www/test.com" ServerName test.com ErrorLog "logs/test-error.log" CustomLog "logs/test-access.log" common </VirtualHost> 如果有遇到拒绝访问,那便是要设置目录访问权限了。可以再httpd.conf 中找到根目录修改 <Directory F:/wamp/www/> AllowOverride All Require all granted </Directory>
甚至你可以把这段代码放到上面的<virtualhost>标签里面。
重启apache。http://test.com 访问ok!
二、nginx
1.host文件
vi /etc/hosts
追加
Include conf/extra/httpd-vhosts.conf
wq保存退出
2.修改nginx配置
vi /etc/nginx/nginx.conf
末端打开加载虚拟配置目录
Include conf/extra/httpd-vhosts.conf
3.添加虚拟配置
vi /etc/nginx/conf.d/test.com.conf
server{ listen 80; server_name test.com; index index.html index.php; root /var/www/html/test.com; #支持php location ~ \.php$ { root /var/www/html/test.com; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PHP_VALUE "open_vasedir=$document_root:/temp;"; include fastcgi_params; } }
重启nginx服务。
service nginx restart
访问test.com,OK!
时间: 2024-10-06 06:03:33