首页 > 解决方案 > 404 Not Found on Laravel when requesting api route-(configuring nginx) in combination with angular

问题描述

我有一个服务器(ubuntu),我在其中部署了 Angular 8 和 Laravel 项目。Laravel 用于 api 请求目的。当我访问我的 ip 地址时,我看到有角度的工作,路由正在工作等。但是例如在调用我的 ipaddress/api/myroute =>(users) 时遇到问题。我认为我的文件中一定有问题:

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;


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

        server_name MY IP ADRESS;

    add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

        charset utf-8; ## Set the charset ##


         location / { ## ANGULAR(SSR-Node)
                root /var/www/html/frontend;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ /index.html;
                proxy_pass http://127.0.0.1:4000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }


       location /api {
                alias /var/www/html/laravel/public; ## Site root for Laravel code ##
                try_files $uri $uri/ /index.php?$query_string;


            location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
            }
        }


    location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        error_page 404 /index.php;

        # All files that end with .php are through fastcgi
        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }



        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
         location ~ /\.(?!well-known).* {
        deny all;
    }
}

这也是我的 routes/api.php 文件:

<?php


/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


/***** USERS ******/

// List of users
Route::get('users', 'UserController@index');

// List of top users
Route::get('users/best', 'UserController@indexTopUsers');


// Update top users
Route::middleware('jwt.auth')->post('updatetopusers', 'UserController@saveTopUsers');


// Store follower to current signed user
Route::middleware('jwt.auth')->post('users/follower', 'UserController@storeFollower');

// Delete follower to current signed user
Route::middleware('jwt.auth')->post('users/unfollower', 'UserController@unFollower');

// Get single user
Route::get('users/{id}', 'UserController@show');

// Get single user profile avatar
Route::get('public/uploads/avatar/{id}', 'UserController@getUserAvatar');

Route::middleware('jwt.auth')->post('users/upload/{id}', 'UserController@uploadAvatar');

//Create new User- SIGN UP USER!
Route::post('user', 'UserController@store');

// Update User
Route::middleware('jwt.auth')->post('users/edit/{id}', 'UserController@update');

// Delete User
Route::middleware('jwt.auth')->delete('users/delete/{id}', 'UserController@destroy');

//SIGN IN USER
Route::post('user/signin', 'UserController@signInUser');

// Get filtered users
Route::get('users/filter/{id}', 'UserController@showFilteredUsers');

// Get list of users that matches some LIKE char
Route::get('/users/queryusers/{char}', 'UserController@queryUsers');

// Get list of users that matches some LIKE char
Route::get('users/search/{char}/{sort}', 'UserController@queryUsersWithParams');

//POST USER AVATAR
Route::middleware('jwt.auth')->post('users/{id}/avatarupload', 'UserController@uploadAvatar');

// Get current logged user
Route::middleware('jwt.auth')->get('user-current', 'UserController@me');


/***** USERS ******/




我从 laravel web 如何设置 nginx 并使用 PHP 7.2.19-0ubuntu0.19.04.2 和 Angular 8(SSR) 组合。

任何帮助都将受到欢迎。

标签: phplaravelnginx

解决方案


我找到了解决方案,如果有人会看的话。上面我使用的是相同的域。现在,我为 api.domain.com 为后端部分创建了子域和单独的 nginx 配置文件,并将这两个文件“移动”到启用站点的位置。希望它可以帮助某人


推荐阅读