Laravel API位于同一IP上,nginx具有不同的端口

时间:2018-07-20 08:29:53

标签: laravel api nginx port config

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

现在,我在/ etc / nginx / sites-available中创建了第二个文件,并链接到启用了sites的文件。在主机中,出于测试目的,我还创建了一行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目的。但是,所以我知道它是否也可以使用,我应该在“公共”文件夹中看到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;
    }
}

1 个答案:

答案 0 :(得分:0)

其中一种方式:

在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/;
       ...