Nginx配置虚拟主机

配置虚拟主机恐怕是许多服务器常用的方式,只需要一个IP就可以划分出多个站点,下面介绍在nginx下如何配置虚拟主机

打开文件/etc/nginx/nginx.conf

vim /etc/nginx/nginx.conf

找到http,在里边加入以下代码

server
{
  listen       80;                        #端口,通常默认就好(必须)
  server_name  www.qttc.net qttc.net;     #域名,多个用空格隔开(必须)
  index index.html index.htm index.php;   #默认首页文档,多个可以增加(必须)
  root  /webs/www.qttc.net;               #虚拟主机目录(必须)

  #--自动在域名前补齐www(非必要) --#
  if ($host != 'www.qttc.net' ) {
    rewrite ^/(.*)$ https://www.qttc.net/$1 permanent;
  }

  #--为WordPress伪静态--(非必要)--#
  if (-f $request_filename/index.html) {
    rewrite (.*) $1/index.html break;
  }

  if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
  }

  if (!-f $request_filename){
    rewrite (.*) /index.php;
  }
  #--WordPress伪静态结束--#

  #--PHP支持--如果你搭建的是PHP环境则此段要加--#
  location ~ .*\.(php|php5)?$
  {
    #fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fcgi.conf;
  }
  #--PHP支持结束--#

  #--额外配置,如果线上运行最好去掉,如第一行代表开开检索(不安全)--#
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
  #--额外配置--#

  #日志打开(最新版本格式)
  access_log  /webs/logs/access_qttc.log  logs;
}

保存重启

nginx -s reload
分享

TITLE: Nginx配置虚拟主机

LINK: https://www.qttc.net/16-nginx-config-server.html

NOTE: 原创内容,转载请注明出自琼台博客