服务2个具有相同IP和端口的项目时发生Nginx错误

首先,如果我的英文不是很好,如果有什么词不能正确理解,我表示歉意。

我正尝试为两个项目提供服务,我在同一台机器上安装了不同的地点。 我想调用这两个项目使用一个单一的IP和端口,但不同的目录:

  • Test.example.com/advanceerp
  • Test.example.com/phpmyadmin

设置应该很容易。 我得到nginx显示我的project1没有问题,但是当试图服务project2表明404错误。 如果我检查日志,我发现当它试图服务这个项目时,它正在查看名为advance的项目的根目录…我已经在我的站点上启用了两个虚拟主机,如下所示:

server { listen 80; listen [::]:80; root /var/www/; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name test.example.com; location /advanceerp { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.1-fpm.sock; } location ~ /\.ht { deny all; } } server { listen 80; listen [::]:80; root /usr/share/phpmyadmin; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name test.example.com; location /phpmyadmin { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } } 

我使用nginx的时间很短所以我明白这肯定是一个初学者的错误,请不要紧记我)什么是最简单的方法来做到这一点? 亲切的问候和感谢提前。


是的,我只简化了启用网站的configuration。 留下一个文件,并在configuration的几个位置指向不同的项目…这也不适用于我,它表明一个404错误。

 Server { Listen 80; Listen [::]: 80 ipv6only = on; Index.php index.html index.htm index.nginx-debian.html; Root / var / www / html; Server_name test.fasttracknet.es; Location / { Try_files $ uri = 404; } Location / advanceerp { root /var/www; Try_files $ uri $ uri / = 404; } Location /phpmyadmin { root /usr/share/phpmyadmin; Try_files $ uri $ uri / = 404; } Location ~ \ .php $ { Include snippets / fastcgi-php.conf; Fastcgi_pass unix: /run/php/php7.0-fpm.sock; } Location ~ /\.ht { Deny all; } } 

test.example.comconfiguration可以使http://test.example.com/phpmyadmin指向/usr/share/phpmyadmin/phpmyadmin

它以这种方式工作,因为您已经将root指定为/usr/share/phpmyadmin并将location/phpmyadmin 。 nginx将location的值附加到root值来确定文件的位置。

在你的情况下,我会使用下面的configurationtest.example.com

 server { listen 80; listen [::]:80; root /path/to/empty/dir; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name test.example.com; location /phpmyadmin { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. alias /usr/share/phpmyadmin; try_files $uri =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } } 

这里我们使用alias指令来指定location /phpmyadmin的确切文件系统path。

/path/to/empty/dir是访问http://test.example.com时要显示的目录。

在两个服务器的server块中都有root指令,所以第二个条目覆盖第一个条目。

将apache的root指令移动到location块中:

 server { server_name test.example.com; root /var/www/; } server { server_name test.example.com; location /phpmyadmin { root /usr/share/phpmyadmin; } } 

注意:您可能会试图将所有的root指令移动到它们的location块中,但nginx文档build议不要这样做 。