Nginx

使用 nginx 創建虛擬主機

  • September 5, 2016

無論我做什麼,我都在使用 nginx 的虛擬主機上遇到問題,我訪問 www/html 而不是 www/example1 文件夾。任何人都可以發現問題嗎?我錯過了什麼嗎?

pi@homeserver:/etc/nginx/sites-enabled $ ls -l
total 4
-rw-r--r-- 1 root root 467 Sep  4 19:41 default
lrwxrwxrwx 1 root root  38 Sep  4 19:43 example1 -> /etc/nginx/sites-available/example1

預設文件是

server { 
 listen 80; 

 root /var/www/html; 
 index index.php index.html index.htm; 

 server_name localhost; 

 location / { 
    try_files $uri $uri/ =404; 
 }

   location ~\.php$ { 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_split_path_info ^(.+\.php)(/.*)$; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_param HTTPS off; 
       try_files $uri =404;
        include fastcgi_params; 
   }
}

範例1文件是

server { 
 listen example1.com:80; 

 root /var/www/example1.com; 
 index index.php index.html index.htm; 

 server_name example1.com www.example1.com; 

 location / { 
    try_files $uri $uri/ =404; 
 }

   location ~\.php$ { 
       fastcgi_pass unix:/var/run/php5-fpm.sock; 
       fastcgi_split_path_info ^(.+\.php)(/.*)$; 
       fastcgi_index index.php; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
       fastcgi_param HTTPS off; 
       try_files $uri =404;
        include fastcgi_params; 
   }
}

我在日誌中收到此錯誤

2016/09/04 21:41:08 [emerg] 1788#0: invalid host in "http://www.example1.com:80" of the "listen" directive in /etc/nginx/sites-enabled/example1.com:2

從錯誤消息來看,似乎nginx很難將該語句解析為listen example1.com:80;IP 地址。

如果您有一台多宿主伺服器並希望將服務限制為一個介面,則為該指令提供 IP 地址listen很有用。

在大多數情況下,聲明listen 80;就足夠了。我建議您對兩個伺服器塊使用以下內容:

server {
   listen 80;
   ...
}

有關更多資訊,請參閱此文件

引用自:https://unix.stackexchange.com/questions/307839