2016-11-18 58 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 –