Nginx configuration with Laravel API + vuejs - laravel

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

Related

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

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;
}
}

Nginx different root for only mainpage

I am trying to configure nginx to set two roots for one domain. My folder structure looks like:
/var/www/
mainpage/
project/index.php
backend/
public/index.php
I need to have root url example.com uses project in folder mainpage and all other urls uses project in folder backend (this is laravel app).
I have started to try to access mainpage project by url example.com/mainpage, but I'm receiving 403 or "Input file not specified". My nginx config:
server {
server_name example.com;
root "/var/www/backend/public";
index index.html index.htm index.php;
charset utf-8;
location /mainpage/ {
alias /var/www/mainpage/project;
try_files $uri $uri/ /index.php$is_args$args;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-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;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
This config return "403 forbidden". How should I configure nginx to work properly? And how to configure it to access mainpage for "/" url and other urls to use backend project
You can set up root directory inside each location e.g.:
location / {
try_files $uri $uri/ /index.php?$query_string;
root /your/root/directory;
}

Docker Laravel Vuejs Nginx, issues loading assets

I can't get my Nginx config to work correctly.
I have a Laravel app, but the /admin uri is redirected by Nginx to a vuejs app.
The VueJS app index.html file is loading fine, but the assets are not, so the app isn't working.
The Laravel App and the VueJS apps are on two separate Docker Containers launched via docker-compose.
I have tried to play around with various combinations of "try_files" inside the "location /admin { }" section with no success so far. These adjustments have either resulted in 404s for everything or 500 Server Errors.
Here is my Nginx conf:
server {
listen 80;
listen [::]:80;
client_max_body_size 200M;
disable_symlinks off;
server_tokens off;
index index.php;
root /var/www/public;
location /admin {
root /var/www/public/dist;
proxy_pass http://admin:8001;
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 / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass http://app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REALPATHTEST $realpath_root;
internal;
}
}
Here are screenshots showing that the index.html has loaded fine, but not the js & css assets:
HTML output in Chrome:
200 Result for page and 404 errors for assets:
Any help would be much appreciated.
In the end all I needed was this:
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://admin:8001;
}
So my final nginx config is:
server {
listen 80 default_server;
listen [::]:80 default_server;
client_max_body_size 200M;
disable_symlinks off;
server_tokens off;
index index.php;
root /var/www/public;
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://admin:8001;
}
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REALPATHTEST $realpath_root;
internal;
}
}
I really hope this saves someone some time!!
If you are using vue-cli to setup your vue project, set baseUrl to /admin
Otherwise you will have to prefix your vue.js generated asset with /admin, or make nginx try files under vue project for every /css and /js requests.

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.

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