登录  /  注册
首页 > web前端 > js教程 > 正文

在Nginx中如何配置多站点vhost

亚连
发布: 2018-06-13 14:05:43
原创
1368人浏览过

这篇文章主要介绍了Nginx 配置多站点vhost 的方法,需要的朋友可以参考下

假设你想在Linux Nginx中用不同的域名访问不同的目录,这时就要配置多个vhost,具体配置如下,假设网站根目录设定在/var/www/

1、在/var/www/下新建两个目录

/var/www/ushark.net
/var/www/ushark.wang
登录后复制

2、编辑/etc/nginx/nginx.conf

http {
  include    /etc/nginx/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"';
  access_log /var/log/nginx/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  keepalive_timeout 65;
  #gzip on;
  include /etc/nginx/conf.d/*.conf;   #主要是加入此行,如有则忽略
}
登录后复制

3、在/etc/nginx/conf.d下新建两个conf文件,

/etc/nginx/conf.d/ushark.net.conf
/etc/nginx/conf.d/ushark.wang.conf
登录后复制

4、复制如下配置信息到两个文件中,只要修改红色部分内容  !!! server_name与root保持一致即目录和域名一一对应 !!!

server {
  listen    80;
  server_name  www.ushark.net;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  root  /var/www/ushark.net/;
  if (!-e $request_filename){   # rewrite可根据网站需要增删
      rewrite ^/(.*) /index.php last; 
  } 
  location / {
    index index.php index.html index.htm;
  }
  #error_page 404       /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /var/www/ushark.net/;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~* \.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;
  }
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
登录后复制

5、重启Nginx

systemctl restart nginx
登录后复制

6、 编辑/etc/hosts  !!! 核心步骤 !!!

[root@bogon ~]# vi 127.0.0.1    localhost.localdomain localhost
::1       localhost6.localdomain6 localhost6
127.0.0.1    www.ushark.net
127.0.0.1    www.ushark.wang
登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在AngularJS中使用ui-route实现多层嵌套路由(详细教程)

有关vue中如何实现二级联动默认选中第一个值

使用js实现推箱子小游戏(详细教程)

以上就是在Nginx中如何配置多站点vhost的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学