Laravel + nginX + Windows 10 + api routing issue (404 Error) - laravel

I am trying to make a very simple API request with above settings. It is working perfectly if I serve it with artisan serve command. But If I make the request with nginx, it gives 404 error. Thats why I guess the issue is with the nginX. my nginX conf is as follows.
server {
listen 80 ;
listen [::]:80 ;
root D:/sites/demo/public;
index index.php index.html index.htm ;
server_name laravel.local;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

Related

Nginx configuration with Laravel API + vuejs

I have the application with frontend on vuejs which works through the laravel api on backend. And I use Laravel Passport for authorization.
When I start the application through the integrated laravel web server (artisan serve) on localhost:8000 it works good without any errors.
But when I moved it to my local web server I got some problems.
When I try to do the post request
axios.post('http://project.local/oauth/token', data)
I get the http error 405 (Method Not Allowed).
I excluded all routes from CSRF-token, added CORS headers and anyway it doesnt work. As I said on localhost:8000 it works well.
I didn't find any information about this problem and couldn't do it by myself. Does anyone know anything about it? Thanks in advance.
My nginx config
server {
listen 80;
root /home/me/project/frontend/dist;
index index.html index.php;
server_name project.local;
access_log off;
error_log /home/me/project/backend/error.log notice;
location / {
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /api/v1 {
root /home/me/project/backend/public;
rewrite ^/api/v1/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location /oauth/token {
root /home/me/project/backend/public;
rewrite ^/oauth/token/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
set $newurl $request_uri;
if ($newurl ~ ^/api/v1(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
if ($newurl ~ ^/oauth/token(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Maybe it's a cache! I just solved this problem and it turned out to be a cache cloudflare
cached all files as index.html

Nginx server laravel route directory 404 error on Windows

I'm getting a 404 every time I try to open a page other than the welcome page. I've read all the posts here on how to fix the problem, but none of the solutions worked for me.
I'm on Windows 10 and use WPN-XM for the Nginx server.
My laravel project is installed in C:/server/www/myapp
My configuration looks as follows:
server {
listen 127.0.0.1:80;
server_name localhost;
root www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_pool;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# mitigate namespace conflict, see httpoxy.org
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
I tried replacing root www with "C:/server/www/myapp/public" and a couple of other options.
I used try_files $uri $uri/ /index.php$is_args$args instead of try_files $uri $uri/ /index.php?$query_string
Any help will be appreciated.

I get a 404 on a laravel app running on Nginx

I have this nginx config.
server {
listen 80;
listen [::]:80;
server_name apps.myapp.com;
root /var/www/apps.myapp.com/public;
index index.php;
location / {
try_files $uri $uri/ index.php?$query_string;
}
}
I have this route:
Route::get('/lead', 'LeadsController#index' );
When I access to http://apps.myapp.com/lead, I get 404.
You can run nginx -t to check if there any error in the file configuration, in addition to that you need to add fastcgi_pass
Below is nginx configuration from my production server
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/public/;
index index.php index.html index.htm;
server_name xxx.xxx.xxx.xxx;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You can read this tutorial is very helpful:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04
This is the configuration I use for my Laravel app.
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$query_string;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

nginx config for two Laravel projects in different location

I'm trying to run two Laravel projects in a different location without using domain name here my config file.
server {
listen 80 default_server;
server_name ip ;
index index.php index.html index.htm;
location /project1 {
root /var/www/project1/public;
try_files $uri $uri/ /index.php?$query_string;
}
location /project2 {
root /var/project2/public;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
That returns 404 for both and from the error log return /(nginx root)/favicon.ico failed to open.
I solved using another port
some thing like that
server {
listen 85 ;
listen [::]:85 ;
server_name ip:85 ;
root /var/www/html/projectfolder1;
}
server {
listen 90 ;
listen [::]:90 ;
server_name ip:90 ;
root /var/www/projectfolder2;
}
hope that help some one

Nested Codeigniter apps not working in NGINX

We've setup 2 CodeIgniter apps which has its own complete code bases on its own directories as in this example. Plus we also have wordpress blog on the same public_html directory.
public_html/HOME_APP
public_html/admin_tool/ADMIN_TOOL_CODES
public_html/blog/WORDPRESS_CODES
HOME_APP codes (CodeIgniter) and our WORDPRESS works fine. But the admin_tool (CodeIgniter) doesn't work. We can access only the http://example.com/admin_tool/index.php but not any inside controller pages. When accessing its show 404 error page. And it looks like the routes are handled via APP1
The nginx rules are as follows. Appreciate if anyone can help us to fix the issue with admin_tool
server{
listen 80;
root /home/ubuntu/websites/example.com/public_html;
index index.html index.htm index.php;
server_name example.com;
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ^~/admin_tool/ {
root /home/ubuntu/websites/example.com/public_html/admin_tool;
index index.php;
try_files $uri $uri/ /index.php$args;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
The following should do the trick for all of your installations:
# We define the index directory at the outermost level and therefore
# only once for all servers. Also note that we use the PHP file first
# because all main directories are handled by PHP scripts and this will
# give us best performance.
index index.php index.html index.htm;
server {
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
# 80 is default!
#listen 80;
root /home/ubuntu/websites/example.com/public_html;
server_name example.com;
location / {
# Don't allow access to the logs directory.
location ~* ^/logs {
return 404;
}
# Don't allow access to PHP files.
location ~* \.php$ {
return 404;
}
# Handle static files.
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# Directly return if the requested URI is a real file.
try_files $uri $uri/ index.php =404;
}
# Codeigniter and WordPress will always handle everything with their
# index.php script, therefore we only need to catch that case.
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
I had the same problem while I am using codeigniter in subfolder(dashboard).
All non-wordpress requests are not being entertained but remain on wordpress main page.
I fixed my issue by following lines in my configuration file /etc/nginx/sites-available/{my-site-name}
location /dashboard {
try_files $uri $uri/ /dashboard/index.php;
}
location /index.php {
fastcgi_pass unix:/usr/sbin/php5-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Resources