2016-11-18 46 views
1

我想使用我的Laravel API作为(示例)apiname.laravelsite.com/v1/api,而不是php artisan serve。目前只从港口提供服务,然后通过apiname.laravelsite.com:8125/v1/api工作访问该端口。Laravel 5 API NGINX没有服务?

有没有人有这方面的经验?我已经尝试设置我的NGINX服务器(使用php 7.0,php-fpm & ubuntu 16.04),但是当我尝试访问我的URL而没有使用端口时,我得到一个404,并且当我访问apiname.laravelsite.com时,我得到一个403 ..

/etc/nginx/sites-available/default

我NGINX配置:

## 
# You should look at the following URL's in order to grasp a solid understanding 
# of Nginx configuration files in order to fully unleash the power of Nginx. 
# http://wiki.nginx.org/Pitfalls 
# http://wiki.nginx.org/QuickStart 
# http://wiki.nginx.org/Configuration 
# 
# Generally, you will want to move this file somewhere, and start with a clean 
# file but keep this around for reference. Or just disable in sites-enabled. 
# 
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. 
## 

# Default server configuration 
# 
server { 
     listen 80 default_server; 
     listen [::]:80 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/dorsia; 

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

     server_name _; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ =404; 
     } 

     # 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: 
      # 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/dorsia; 

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

      server_name _; 

      location/{ 
        # First attempt to serve request as file, then 
        # as directory, then fall back to displaying a 404. 
        try_files $uri $uri/ =404; 
      } 

      # 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 45.55.184.33:8125; 
      #  # With php7.0-fpm: 
      #  fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
      #} 

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


    # Virtual Host configuration for example.com 
    # 
    # You can move that to a different file under sites-available/ and symlink that 
    # to sites-enabled/ to enable it. 
    # 
    #server { 
    #  listen 80; 
    #  listen [::]:80; 
    # 
    #  server_name example.com; 
    # 
    #  root /var/www/example.com; 
    #  index index.html; 
    # 
       #  fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
       #} 

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


     # Virtual Host configuration for example.com 
     # 
     # You can move that to a different file under sites-available/ and symlink that 
     # to sites-enabled/ to enable it. 
     # 
     #server { 
     #  listen 80; 
     #  listen [::]:80; 
     # 
     #  server_name example.com; 
     # 
     #  root /var/www/example.com; 
     #  index index.html; 
     # 
     #  location/{ 
     #    try_files $uri $uri/ =404; 
     #  } 
     #} 
+0

你可以考虑使用宅基地。它使工作流程更加容易。 – ssuhat

+0

不错,考虑到它,除了我已经建立了我的API,已经安装了node,php 7,composer等。是不是使用宅基地,然后太麻烦了? @SSuhat –

回答

3

我觉得象下面这样的配置将正常工作。

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 
    server_name _; 

    charset utf-8; 
    root "/var/www/dorsia/public"; 

    access_log /var/log/nginx/dorsia-acc.log; 
    error_log /var/log/nginx/dorsia-err.log; 

    location/{ 
    try_files $uri $uri/ /index.php?$query_string 
    } 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location = /robots.txt { access_log off; log_not_found off; } 
    location ~ /\.ht  { deny all; } 

    location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_intercept_errors off; 
    fastcgi_buffer_size 16k; 
    fastcgi_buffers 4 16k; 
    } 
} 

编辑:也确保正确的用户/组设置在fpm的配置,以便它可以访问并运行PHP文件。

+0

谢谢,我会试试这个让你知道结果。顺便说一句,这个用户是根,是一个问题? –

+0

@FabianTjoeAOn它肯定会访问文件比。但考虑在生产时将其更改为应用程序的特定用户。 – balintant

+0

顺便说一句,这是不适用于任何网络路线,我的laravel只是休息API,我的前端可能是另一个域上的客户端,所以我不应该指向我的公共文件夹吗? @balintant –