Laravel Route in NGINX on Windows machine didn't work well - laravel

I just install my nginx server on my windows laptop. Then I setup the nginx.conf file like this :
server {
listen 80;
server_name laravelninja.local;
root C:/blablabla/public;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
running the php-cgi using this syntax php-cgi.exe -b 127.0.0.1:9000
and the hosts also
127.0.0.1 laravelninja.local
It's running well at laravelninja.local/, but when I go to other route like laravelninja.local/pizzas, this error came out from nginx
2020/12/23 21:26:52 [error] 8980#11972: *7 CreateFile() "C:/blablabla/public/pizzas" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: laravelninja.local, request: "GET /pizzas HTTP/1.1", host: "laravelninja.local"
and the browser goes to google and search the laravelninja.local/pizzas
this is the code in my route :
Route::get('/', function () {
return view('welcome');
});
Route::get('/pizzas', function () {
return view('pizzas');
});
and the same level view of pizzas.blade.php as welcome.blade.php on the views folder.
is there any other option to solve this problem except using laragon ?

Add these 2 blocks in your config like this:
server {
#
# your configs...
#
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
Restart your NGINX server.
(I'm not sure about your other configs you have, but I believe the first block is what you want.)

Related

Nested Laravel-inertia-react App using nginx+phpfpm

I'm currently building a server running multiple laravel apps with inertia-vite-react stack, the file structure is like this:
/var/www/app1
/var/www/app2
this is my nginx config:
location /app1 {
alias /var/www/app1/public/;
try_files $uri $uri/ #app1;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location #app1 {
rewrite /app1/(.*)$ /app1/index.php?/$1 last;
}
and I'm facing a problem that Inertia(maybe?) keep adding the app1 after the url, like this:
http://localhost:8080/app1 becomes http://localhost:8080/app1/app1
the page renders correctly when I enter http://localhost:8080/app1,
but was changed automatically and returning 404 after I refresh with the changed url. So I'm guessing this is a inertia issue or somewhere wrong with my nginx config.

Multiple Laravel Applications Using Nginx - Windows

I have two different laravel application on my server machine.
They are located at:
D:/APPLICATION/application1
and
D:/APPLICATION/application2
Below is my nginx.conf content:
server {
listen 80;
server_name localhost;
location / {
root "D:/APPLICATION/application1/public";
try_files $uri $uri/ /index.php?$query_string;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ^~ /application2 {
alias "D:/APPLICATION/application2/public";
try_files $uri $uri/ /index.php?$query_string;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
If I browse http://x.x.x.x/, my first laravel web application comes out perfectly.
But if I browse http://x.x.x.x/application2 I am having No input file specified.
Anything I am missing here?
For windows use fastcgi_pass as 127.0.0.1:9000 instead of unix socket.
Please make sure your php cgi is running. If not, you can start it by
1. Open command prompt
2. Go to path of php-cgi file. (e.g. C:\php-7.3.11, here you'll find fast-cgi.exe).
2. php-cgi.exe -b 127.0.0.1:9000
Nginx configuration with rewrite module.
# Nginx.conf
# App 1(Path: D:/APPLICATION/application1, Url: http://localhost)
# App 2(Path: D:/APPLICATION/application2, Url: http://localhost/application2)
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name localhost;
# Default index pages
index index.php;
# Root for / project
root "D:/APPLICATION/application1/public";
# Handle main root / project
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle application2 project
location /application2 {
# Root for this project
root "D:/APPLICATION/application2/public";
# Rewrite $uri=/application2/xyz back to just $uri=/xyz
rewrite ^/application2/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# We don't want to pass /application2/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# So laravel route('/xyz') responds to /application2/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/application2(.*)$) {
set $newurl $1;
root "D:/APPLICATION/application2/public";
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fastcgi rather than php fpm sock
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}
Note: When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname localhost as root url not localhost/application2. To resolve this issue please do following changes in laravel app.
Define APP_URL in .env file as APP_URL="localhost/application2"
Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}
Update: Make sure to run php artisan config:clear or php artisan config:cache command to load the updated value of APP_URL.
For Linux System : Nginx: Serve multiple Laravel apps with same url but two different sub locations in Linux

Vue router server configuration for history mode on nginx does not work

I read the following note from vue router documentation
Note: when using the history mode, the server needs to be properly configured so that a user directly visiting a deep link on your site
doesn't get a 404.
So, I try to configure my nginx like the following
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 working.dev;
location /user {
rewrite ^(.+)$ /index.php last;
}
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;
}
}
And the following is my Vue configuration
var router = new VueRouter({
hashbang: false,
history: true,
linkActiveClass: "active",
root: '/user'
});
But, I still got 404 when user directly visiting a deep link in my site.
Edited: I use Laravel routing too. The Following is my laravel routing.
Route::get('user', function() {
return View::make('user.index');
});
I just read mattstauffer blog post and finally found way to do in Laravel route. Like the following
Route::get('user/{vue_capture?}', function() {
return View::make('user.index');
})->where('vue_capture', '[\/\w\.-]*');
It does not return 404 when user directly visiting a deep link to site.

Nginx rewrite not working at all

I am trying to set up a Piwigo to run on Nginx but I got into some issues with links that should contain and index.php like this:
foto.domain.net/index.php/category/3
It seems like a simple task, rewriting /index/category/3 to /index.php/category/3, but no matter what I write in those rewrites, I end up with a 404 error and a never changing error log entry that looks like this:
open() "/srv/http/foto/index/category/3" failed (2: No such file or directory), client: 94.242.246.23, server: ~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$, request: "GET /index/category/3 HTTP/1.1", host: "foto.domain.net", referrer: "http://foto.domain.net/"
The error log looks the same even if I add junk to the rewrite line such as:
rewrite ^/index((/|$).*)$ /JUNK_index.php$1 last;
So the rewrite part is faulty, somehow but I can't figure it out...
server {
listen 80;
server_name domain.net;
return 301 $scheme://www.domain.net$request_uri;
}
server {
listen 80;
server_name "~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$";
root /srv/http/$subdomain;
location #rewrites {
rewrite ^/picture((/|$).*)$ /picture.php$1 last;
rewrite ^/index((/|$).*)$ /index.php$1 last;
# The following is needed for batch operations which use i.php
rewrite ^/i((/|$).*)$ /i.php$1 last;
}
location /$subdomain {
index index.php index.html;
try_files $uri $uri.php $uri/ #rewrites;
}
location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
try_files $script_name = 404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
In general terms, as a checklist:
make sure the target/landing URI exists
this should have output "rewrite": nginx 2>&1 | grep -i -o rewrite
check your error and access log

Rewriting URI within Nginx

Im trying to get URI Routing set up for my framework and im currently working on nginx as teh server, but the problem is i keep getting 500 error when trying to access either one of the following links
http://localhost.framework/
http://localhost.framework/index.php/
If I access the site using the following links it works:
http://localhost.framework/index.php
http://localhost.framework/index.php?/
my configuration for the domain is as follows:
server {
listen 80;
server_name localhost.framework;
root /var/www/ASFramework;
access_log /var/log/nginx/framework.access.log;
error_log /var/log/nginx/framework.error.log;
location / {
rewrite ^/(.*)$ /index.php/$1 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
}
basically what im trying to do is take the following url
http://localhost.framework/controller/method/../
and rewrite it to:
http://localhost.framework/index.php/controller/method/../
Within the logs (error.log) is:
2011/07/03 22:57:22 [error] 19837#0: *6 rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/", client: 127.0.0.1, server: localhost.framework, request: "GET / HTTP/1.1", host: "localhost.framework"
Can anyone tell me what's going on and how I can fix it?
Change this line:
location ~ \.php$ {
into this:
location ~ \.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
Your rewrite rule causes redirection cycle. nginx substitutes index.php with index.php/index.php recursively. So after second replacement your new URL will be index.php/index.php/index.php and so on.
You probably want something like this:
location / {
rewrite ^/index.php\?action=(.*)$ /$1 last;
}
Which rewrites index.php?action=someaction to /someaction.
Try this:
location / {
if ($request_uri !~ "/(index\.php)") {
rewrite ^/(.*)$ /index.php/$1 last;
}
}

Resources