首页 > 解决方案 > 相同 IP 上的 Laravel API 与 nginx 上的不同端口

问题描述

我有一个运行 nginx 服务器的虚拟机。现在只有一个实例在运行,我想要第二个,用于我的 Laravel API。VM 有 Ubuntu 服务器 16.04 作为操作系统。

现在我在 /etc/nginx/sites-available 中创建了第二个文件并链接到启用了站点。在主机中,出于测试目的,我还创建了一行 127.0.0.1 example.com。如果我用 curl example.com:8000 调用它。我是否在终端中返回一个网站。由于我一次又一次地在那里发现我的文件夹名称,我认为一切都是正确的。

现在我想从外部做到这一点。我希望能够使用不同的端口在 vm 之外访问我的 Laravel API。

我认为如果我有它就像在默认文件中一样有效。所以我已经指定为服务器名称_。那不是解决方案。

使用 192.168.2.110 我仍然可以获得 WordPress 页面。但是对于 192.168.2.110:8000 找不到页面。最后,IP 还应该仅用于 Angular 中的 REST 目的。但是我知道它是否也有效,我应该在 Public 文件夹中看到 Laravel 主页。

我正在关注本教程:https ://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

# Default server configuration
#
server {
    listen 8000 default_server;
    listen [::]:8000 default_server;


    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/MT_Backend_Iventorysystem/public;

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

    server_name _;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    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$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    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.2-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }

    # Following location added to handle WordPress correctly
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robot.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
}

标签: laravelapinginxportconfig

解决方案


方法之一:

在 App/Providers/RouteServiceProvider 中:

public function map()
{
   switch(request()->getPort())
   {
      case 80:
      case 8080:
      case 443:
          $this->mapWebRoutes();
          break;
     // choose a port that is not used by another server
      case 8975:
          $this->mapApiRoutes();
          break;
    }
}

在 ngnix 中

server {
       listen 8975;
       server_name YOUR_DOMAIN_NAME;
       root '/path/to/your/application/public/;
       ...

推荐阅读