用于在 Ubuntu 16.04 上托管多个网站的 NGINX 服务器块配置

用于在 Ubuntu 16.04 上托管多个网站的 NGINX 服务器块配置

我想在 Ubuntu 16.04 (Ubuntu-NGINX-MariaDB-PHP) 上托管多个 wordpress 网站。我不想使用 wordpress 多站点。

我跟着本指南。一切都很好,但我只能托管一个网站。每当我创建多个服务器块配置时,它就会开始显示错误,并且 NGINX 无法启动。我没有得到正确的配置文件。这是配置文件:

server {
     listen [::]:80 ipv6only=off;
     server_name abcde.org www.abcde.org;

     root /var/www/abcde;

    # Add index.php to the list if you are using PHP
    index index.php     index.html index.htm index.nginx-debian.html;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                root /usr/share/nginx/html;
        }

     location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
     }


     location ~ /\.ht {
        deny all;
     }
}

如果我只托管一个网站,它工作正常。但是一旦我托管另一个网站,NGINX 就无法启动。在更改服务器名称和根目录后,我对两个网站使用相同的配置。

请指导我正确配置 NGINX 服务器块。

答案1

你的 nginx.config 需要类似下面这行的内容。这是我在服务器上执行的操作

include /etc/nginx/enabled-sites/*;

在该目录中,您可以拥有一个包含多台服务器的文件,或者您可以按照我的做法,按域对服务器进行分组。

文件 abcde.conf

server {
  listen 80;
  server_name www.abcde.org;

  root /var/www/home;

  # Any locations you want. PHP example that I use below.
  location ~ \.php$ {
    fastcgi_keep_conn on;
    fastcgi_intercept_errors on;
    fastcgi_pass   php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
} # ends www.abcde.org server

# This server forwards to the www domain
server {
  listen 80;
  server_name abcde.org;
  return 301 https://www.abcde.org$request_uri;
} # ends abcde.org server

文件 example.conf

# server for a completely separate domain
server {
  listen 80;
  server_name www.example.com;

  root /var/www/example;

  # Any locations you want
} # ends www.example.com server

文件 default_server.conf

# This just prevents Nginx picking a random default server if it doesn't
# know which server block to send a request to
server {
  listen      80 default_server;
  server_name _;
  # This means "go away", effectively. You can also forward somewhere
  # or put default_server onto any of your server blocks.
  return      444; 
}

相关内容