【CentOS 7架构22】,设置默认虚拟主机#

本文详细介绍了如何使用Nginx配置默认虚拟主机的过程,包括修改配置文件、创建虚拟主机目录、设置监听端口、指定服务器名称及根目录等步骤,并通过curl命令验证配置是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shallow丿ove


默认虚拟主机

  • vi /usr/local/nginx/conf/nginx.conf include vhost/*.conf
  • mkdir /usr/local/nginx/conf/vhost
  • cd !$ ; vi default.conf server { listen 80 default_server; #标记为默认虚拟主机 server_name aaa.com index index.html index.htm index.php;; root /data/wwwroot/default; }
  • mkdir -p /data/wwwroot/default/
  • echo "This is a default site." > /data/wwwroot/default/index.html
  • /usr/local/nginx/sbin/nginx -t
  • /usr/local/nginx/sbin/nginx -s reload
  • curl localhost
  • curl -x 127.0.0.1:80 123.com
[root@localhost conf]# vi nginx.conf
     36     fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
     37     fastcgi_intercept_errors on;
     38     tcp_nodelay on;
     39     gzip on;
     40     gzip_min_length 1k;
     41     gzip_buffers 4 8k;
     42     gzip_comp_level 5;
     43     gzip_http_version 1.1;
     44     gzip_types text/plain application/x-javascript text/css text/htm
     45     application/xml;
     46     include vhost/*.conf;
     47 }

将 46 server 47 { 48 listen 80; 49 server_name localhost; 50 index index.html index.htm index.php; 51 root /usr/local/nginx/html; 52 location ~ .php$ 53 { 54 include fastcgi_params; 55 fastcgi_pass unix:/tmp/php-fcgi.sock; 56 fastcgi_index index.php; 57 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; 58 } 59 } 改为include vhost/*.conf;

[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# mkdir vhost
[root@localhost conf]# cd vhost/
[root@localhost vhost]# vi aaa.com.conf
      1 server
      2 {
      3         listen 80 default_server;
      4         server_name aaa.com;
      5         index index.html index.htm index.php;
      6         root /data/wwwroot/default;
      7 }

[root@localhost vhost]# mkdir /data/wwwroot/default [root@localhost vhost]# cd !$ cd /data/wwwroot/default [root@localhost default]# vi index.html 1 This is the default site. [root@localhost default]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost default]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful



[root@localhost default]# curl localhost This is the default site. [root@localhost default]# curl -x 127.0.0.1:80 aaa.com This is the default site. [root@localhost default]# curl -x 127.0.0.1:80 bbb.com This is the default site.


> 通过vost目录中查找位于第一个文件为默认虚拟主机访问的页面



---

转载于:https://my.oschina.net/u/3892756/blog/3069364

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值