nginx入门级配置案例

一个虚拟主机

配置单个虚拟主机,即访问到该机器的请求,会被指定到某一个固定的本地目录。

http {
    server {
        listen          80 default;
        server_name     _ *;
        access_log      logs/default.access.log main;
        location / {
            index index.html;
            root  /var/www/default/htdocs;
        }
    }
}

server_name 配成 _ * 表示忽略域名,即所有域名或IP均等同处理。
location后面的 / 表示根路径,里面的index表示首页是index.html,root表示指定到本地某个目录。

多个虚拟主机

这里以2个虚拟主机为例,分别是两个域名的请求被指到不同的虚拟主机。

http {
    server {
        listen          80;
        server_name     www.domain1.com;
        access_log      logs/domain1.access.log main;
        location / {
            index index.html;
            root  /var/www/domain1.com/htdocs;
        }
    }
    server {
        listen          80;
        server_name     www.domain2.com;
        access_log      logs/domain2.access.log main;
        location / {
            index index.html;
            root  /var/www/domain2.com/htdocs;
        }
    }
}

主要是server_name的配置和root的配置不一样,对比单个虚拟主机的配置看,上面的配置也算是够清晰的了。

在父文件夹中建立子文件夹以指向子域名(进阶学习)

server {
    # 根据你的需求改变此端口
    listen       80;  #也可以是1.2.3.4:80的形式
    # 多个主机名可以用空格隔开,当然这个信息也是需要按照你的需求而改变的。
    server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;
    #或者可以使用:_ * (具体内容参见本维基其他页面)
    root /PATH/TO/WEBROOT/$host;
    error_page  404   http://yourdomain.com/errors/404.html;
    access_log  logs/star.yourdomain.com.access.log;
    location / {
        root   /PATH/TO/WEBROOT/$host/;
        index  index.php;
    }
    # 直接支持静态文件 (从配置上看来不是直接支持啊)
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log        off;
        expires           30d;
    }
    location ~ .php$ {
        # 如果需要,你可以为不同的FCGI进程设置不同的服务信息
        fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors on;
    }
    location ~ /\.ht {
        deny  all;
    }
}

这里面用到fastcgi,需要的可以进一步研究学习。
本文参考链接:http://www.nginx.cn/doc/example/fullexample.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值