Ubuntu

用於在 Ubuntu 16.04 上託管多個網站的 NGINX 伺服器塊配置

  • November 22, 2016

我想在 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 伺服器塊。

您的 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; 
}

引用自:https://serverfault.com/questions/816246