只用改一个地方,在http模块里面加入你要引用的虚拟主机配置文件目录即可:
例如:include /usr/local/nginx/default.d/*.conf;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /usr/local/nginx/default.d/*.conf; //就这里加一行就可以了
} |
2、建立虚拟主机配置目录:
在/usr/local/nginx/下面建立default.d文件夹,专门放置网站的配置文件。
贴一个我的配置上来:
/usr/local/nginx/default.d/mytest.conf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
server {
listen 80 ; //注意这里,要把默认的那个default_server去掉,因为我们在下面要单独配置域名访问,所以这里不要留default_server,不然会报错。
server_name mytest.com; //这里写你想设置的域名,可以写多个,与名之间用空格隔开
root /mnt/share/mytest.com; //这里是你虚拟机的根目录,写绝对路径
# Load configuration files for the default server block.
location / {
index index.php index.html index.htm; //这里配置默认访问的页面
}
location ~* \\.php$ { //这里配置php解析.php文件
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
error_page 404 /404.html; //默认的错误页面
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} |
再来一个:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
server {
listen 80;
server_name www.mytest1.com;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico {
log_not_found off;
}
location ~ \\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} |
3、还想再建其他虚拟机,一样的,复制上面那个文件,修改我标注的的那几个地方即可!
4、虚拟机配置文件配置好了之后,还需要在linux下面的hosts文件下面加上以上域名,不然还是会访问外网的哦。
|
1
2
3 |
vim /etc/hosts
127.0.0.1 mytest1.com
127.0.0.1 mytest.com |
5、如果我们是要在windows下面访问虚拟机里面的linux下面的网站,那么还要在windows下面配置hosts文件,所有的域名都指向linux服务器,例如:
|
1
2 |
192.168.0.27 mytest1.com
192.168.0.27 mytest.com |
6、遇到的问题:
|
1 |
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/vhosts/ |
遇到这个问题,肯定是:
|
1
2 |
server {
listen 80 |
这个地方80后面的东西都去掉,只留下端口号80,去掉就可以解决这个问题了。
总结
以上所述是小编给大家介绍的nginx配置多个站点共用80端口的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:https://blog.csdn.net/bbwangj/article/details/80648680
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 67
-
微软改善 Linux 内核 6.6 版本中对于 Hyper-V 的支持,可带来额外安全性
2025-05-27 32 -
美国服务器搭建蜘蛛池业务的优势(美国服务器搭建蜘蛛池业务的优势有哪些呢)
2025-05-25 97 -
2025-06-04 95
-
2025-05-29 76

