Nginx 配置多站点vhost 的方法

 更新时间:2018年01月07日 11:46:45   作者:gentsir  
这篇文章主要介绍了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

总结

以上所述是小编给大家介绍的Nginx 配置多站点vhost 的方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • NodeJS学习笔记之Http模块

    NodeJS学习笔记之Http模块

    这里只是熟悉nodejs中的http模块的API,一般在开发过程中使用的是第三方的框架,比如说Express。其中封装了更为简单的构建http服务器的API。
    2015-01-01
  • windows如何把已安装的nodejs高版本降级为低版本(图文教程)

    windows如何把已安装的nodejs高版本降级为低版本(图文教程)

    这篇文章主要介绍了windows如何把已安装的nodejs高版本降级为低版本,本文分步骤通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 详解node单线程实现高并发原理与node异步I/O

    详解node单线程实现高并发原理与node异步I/O

    本篇文章主要介绍了node单线程实现高并发原理与node异步I/O ,具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • nodejs安装与卸载图文教程(高版本降为低版本)

    nodejs安装与卸载图文教程(高版本降为低版本)

    我们往往用命令行安装的nodejs不是最新版本,而且升级nodejs并不是件非常简单的事情,这篇文章主要给大家介绍了关于nodejs安装与卸载(高版本降为低版本)的相关资料,需要的朋友可以参考下
    2023-12-12
  • 基于node.js制作简单爬虫教程

    基于node.js制作简单爬虫教程

    这篇文章主要为大家详细介绍了基于node.js制作简单爬虫的教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • node.js爬虫框架node-crawler初体验

    node.js爬虫框架node-crawler初体验

    这篇文章主要介绍了node.js爬虫框架node-crawler的相关资料,帮助大家利用node.js进行爬虫,感兴趣的朋友可以了解下
    2020-10-10
  • node.js-v6新版安装具体步骤(分享)

    node.js-v6新版安装具体步骤(分享)

    下面小编就为大家带来一篇node.js-v6新版安装具体步骤(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Nodejs 和Session 原理及实战技巧小结

    Nodejs 和Session 原理及实战技巧小结

    这篇文章主要介绍了Nodejs 和Session 原理及实战技巧小结,需要的朋友可以参考下
    2017-08-08
  • 浅谈使用nodejs搭建web服务器的过程

    浅谈使用nodejs搭建web服务器的过程

    这篇文章主要介绍了浅谈使用nodejs搭建web服务器的过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 浅谈Nodejs中的作用域问题

    浅谈Nodejs中的作用域问题

    在JS中有全局作用域和函数作用域,而在Nodejs中也自己的作用域,分为全局作用域(global)和模块作用域。本文将对Nodejs中的作用域进行介绍,需要的朋友一起来看下吧
    2016-12-12

最新评论