I need help running Laravel from a subfolder in our dev environment.
Our devs have to clone the main site repo into a folder under their subdomain like so:
https://developer.example.com/branch/ points to /var/www/html/developer/branch/
Examples:
https://dev1.example.com/laravel8/
https://dev2.example.com/ticket-12345/
https://dev3.example.com/some-other-branch/
We currently run a homegrown PHP app that this works just fine with, trying to migrate to Laravel (testing with v8.0) I'm finding all routes are broken unless they contain the subfolder name which is not feasible.
// Doesn't work
Route::get('/', function () {
return view('welcome');
});
// Works
Route::get('/laravel8', function () {
return view('welcome');
});
// Doesn't work
Route::get('/welcome', function () {
return view('welcome');
});
// Works
Route::get('/laravel8/welcome', function () {
return view('welcome');
});
// Legacy route handler (Works)
Route::any('/{path?}', LegacyController::class)->where('path', '.*');
I've tried setting the APP_URL to https://dev1.example.com/branch/ which does nothing.
Our NGINX config rewrites requests like so
https://developer.example.com/branch/ -> https://developer.example.com/branch/public/index.php
server {
server_tokens off;
listen 80;
autoindex off;
server_name dev1.example.com;
root /var/www/html/dev1;
access_log /home/dev1/nginx-access.log;
error_log /home/dev1/nginx-error.log debug;
rewrite_log on;
index index.php index.html;
client_max_body_size 100M;
# Rewrite legacy app URLs
# /branch/control.php/login/ -> /branch/login/
#if ($uri ~* "^/([A-Za-z0-9\-\_]+)/control\.php/(.*)$") {
location ~* ^/([A-Za-z0-9\-\_]+)/control\.php/(.*)$ {
rewrite ^/([A-Za-z0-9\-\_]+)/control\.php/(.*)$ /$1/$2; # Works
}
#include /etc/nginx/includes/dev_laravel.conf;
# Capture the subfolder name to a variable
location ~* ^/(?<appid>[A-Za-z0-9\-\_]+) {
alias /var/www/html/dev1/$appid/public;
try_files $uri $uri/ #rewapp;
# Sometimes only contains /branch/login/ other times contains /branch/public/index.php/login/
location ~* ^/([A-Za-z0-9\-\_]+)/(.*\.php)(/.*) {
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $2;
fastcgi_param SCRIPT_FILENAME /var/www/html/dev1/$1/public/$2;
fastcgi_param PATH_INFO $3;
keepalive_timeout 0;
fastcgi_connect_timeout 120;
fastcgi_send_timeout 3600;
fastcgi_read_timeout 3600;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 256k;
fastcgi_intercept_errors off;
#fastcgi_param SCRIPT_FILENAME $document_root$script;
#fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS on;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
include fastcgi_params;
}
# Necessary for static files,
# or may fall to another location with incorrect path
# Add more extensions like PDF if necessary
location ~* ^/([A-Za-z0-9\-\_]+)/(.+\.(jpg|jpeg|gif|css|svg|png|js|map|ico|html|xml|txt|woff|woff2))$ {
alias /var/www/html/dev1/$1/public;
try_files $uri /$2 =404;
}
}
# Rewrite URL for Laravel
# https://www.example.com/branch/login/ -> https://www.example.com/branch/public/index.php/login/
location #rewapp {
#rewrite ^/([A-Za-z0-9\-\_]+)/(?:control\.php/)?(.*)$ /$1/index.php/$2 last; # Try to eliminate control.php (Throws file not found)
rewrite ^/([A-Za-z0-9\-\_]+)/(.*)$ /$1/index.php/$2 last;
}
}
Any help is appreciated.
Related
I am a web developer and I build websites. But I have the following problem:
Since I can work from home (like probably most of us devs can do), I want to use nginx with php to continue working on websites. At work, one of my colleagues gave me one nginx configuration file which can serve multiple sites. I will post the whole code block below.
The problem is that I am working on windows 10 and I do not know how to make this code work on my machine.
My php version is PHP 7.3.12 which is located under C:\php.
My nginx version is 1.16.1 which is located under C:\nginx-1.16.1.
I appreciate every help I can get!
server {
index index.php index.html index.htm;
set $basepath "/home/nik/Code/";
set $domain $host;
set $dynroot "/var/www";
# check multi name domain to multi application
if ($domain ~ "^(.*)\.(.[^.]*)\.(site|vpnsite)$") {
set $customer $1;
set $developer $2;
set $rootpath "${developer}/${customer}/";
set $servername "${customer}.${developer}.site";
set $dynroot "$basepath/$rootpath";
}
if ($domain ~ "^(.*)\.(.[^.]*)\.laravel$") {
set $customer $1;
set $developer $2;
set $rootpath "${developer}/${customer}/";
set $servername "${customer}.${developer}.laravel";
set $dynroot "$basepath/$rootpath/public";
}
client_max_body_size 134M;
server_name "dev"
access_log "/home/nik/Code/App/nginx-logs/${servername}.access.log";
error_log "/home/nik/Code/App/nginx-logs/combined.error.log";
root $dynroot;
# check file exist and send request sting to index.php
location / {
try_files $uri $uri/ /index.php?$args;
autoindex on;
}
# allow execute all php files
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
# configure phpmyadmin path
location /phpmyadmin {
root /usr/share/;
index index.php;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# disallow access to apache configs
location ~ /\.ht {
deny all;
}
# disallow access to git configs path
location ~ /\.git {
deny all;
}
}
I have a pretty standard setup with a symfony2-like app with a front controller, running on nginx 1.10 and Centos7. It all works as expected, blocks where expected etc.
server {
listen 80;
root /opt/my/code/web;
index app.php;
charset utf-8;
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
# pass the PHP scripts to php5-fpm
location ~ ^/app\.php(/|$) {
# problem here
location ~ ^/recording {
add_header Content-Type audio/x-wav;
}
fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index app.php;
include /etc/nginx/fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Prevents URIs that include the front controller. This will 404:
internal;
}
# return 404 for all other php files not matching the front controller
location ~ \.php$ {
return 404;
}
}
I have a few issues but the main one is that I want special handling for a URI matching /recording but it still has to go through the front controller. (This is not debatable, it HAS to go through the front controller and modify a response header if the URI matches /recording)
Since try_files redirects to location ~ ^/app\.php(/|$) nginx's $uri parameter used for location matching gets updated to /app.php, so any nested locations won't work.
I cant use add_header outside of the front controller block because any add_header directives get dropped on an internal redirect.
Obviously I can't use location if with add_header either.
This is easy in apache, but the only remote solution I have found uses a third party lua module and the installation docs are a bit thin on that and the thought of compiling that in from source on centos is giving me heart palpitations.
If internal redirect bother us, lets remove internal redirect :) You can solve it easy with fastcgi config duplication
server {
listen 80;
root /opt/my/code/web;
index app.php;
charset utf-8;
location / {
try_files $uri $uri/ /app.php$is_args$args;
}
location ~ ^/recording {
add_header Content-Type audio/x-wav;
fastcgi_pass unix:/var/run/php-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /app.php;
fastcgi_param SCRIPT_FILENAME $document_root/app.php;
}
# pass the PHP scripts to php5-fpm
location ~ ^/app\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index app.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Prevents URIs that include the front controller. This will 404:
internal;
}
# return 404 for all other php files not matching the front controller
location ~ \.php$ {
return 404;
}
}
Second solution works only if you know content type of all other requests. We can use variables. (btw, I don't suggest this solution because harder to support and not cute :))
server {
listen 80;
root /opt/my/code/web;
index app.php;
charset utf-8;
location / {
try_files $uri $uri/ /app.php$is_args$args;
set $ct "text/html";
}
location ~ ^/recording {
try_files $uri $uri/ /app.php$is_args$args;
set $ct "audio/x-wav";
}
# pass the PHP scripts to php5-fpm
location ~ ^/app\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/?.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index app.php;
include /etc/nginx/fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
add_header "Content-Type $ct;
# Prevents URIs that include the front controller. This will 404:
internal;
}
# return 404 for all other php files not matching the front controller
location ~ \.php$ {
return 404;
}
}
I have just played with Laravel some hours ago and seemed to hit a wall. I have seen similar questions here but they're about Apache
Route::get('/', function()
{
return 'hello world';
});
The routing above worked perfectly for localhost/helloworld/public/, however when I changed to this
Route::get('about', function()
{
return 'this is about';
});
and I used this url localhost/helloworld/public/about, it kept displaying 404 Not Found. Even when I tried localhost/helloworld/public/index.php/about, it still didn't work.
This is my nginx default.conf
And this is my .htaccess
I have heard about enabling mod_rewrite on nginx but have't got a clue how to do that.
Please help. Thank you in advance.
Refrence taken from here
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
My nginx configuration for laravel
server {
listen 80;
server_name app.dev;
rewrite_log on;
root /var/www/l4/angular;
index index.html;
location /{
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
location /lara/ {
index index.php;
alias /var/www/l4/public/;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ ^/lara/(.*\.php)$ {
alias /var/www/l4/public/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Laravel route:
Route::get('/', function()
{
return View::make('index');
});
Route::get('x', function()
{
return "alpha";
});
my problem is,"http://app.dev/lara/index.php" is working but "http://app.dev/lara" and lara/x is not working.
In a nutshell, make the following edits. An explanation of why is below.
Replace
try_files $uri $uri/ /index.php?$query_string;
with
try_files $uri $uri/ /lara/index.php?$query_string;
Replace the last location directive with this
location ~ /lara/(.*)$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
fastcgi_param REQUEST_URI /$1;
}
Restart nginx.
Now the why. I spotted a couple of mistakes with your nginx config. First, /index.php?$query_string in the try_files directive should be /lara/index.php?$query_string, otherwise nginx will try a request like http://app.dev/lara as /var/www/l4/angular/index.php?, which leads no where (unless you have an index.php there, and even is it will be served as text, not through fpm).
The second has to do with the location ~ ^/lara/(.*\.php)$ directive. I think restricting it to URIs that end with .php is wrong, because it won't work for http://app.dev/lara/x, which will make nginx only search for /var/www/l4/public/x, returning 404 of course. Changing the regex to ^/lara/(.*)$ should do the job of catching /lara/x. Now the fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; directive is erroneous because for http://app.dev/lara/x, SCRIPT_FILENAME is /var/www/l4/public/x/lara/x, and removing the $1 in the alias directive won't make it any better. Instead, make the fastcgi_param like this fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";, remove the alias directive, it's useless now, then move include fastcgi_params; above the fastcgi_param so it won't override SCRIPT_FILENAME value.
Done? Not yet :). Trying /lara/x will show a Laravel routing error, because it tries to find the route lara/x instead of x, this is because you're including fastcgi_params. Just add fastcgi_param REQUEST_URI /$1; after SCRIPT_FILENAME param directive. Now it should be working fine. Don't forget to restart nginx :).
Okay, i setup a debian install with ispconfig3 ,php5-fpm and nginx, the main site, drupal is working fine, even perfect. But i also have a modded phpbb3 board, in a subdir, named 'forum'.
Now for some reason i get the following error from nginx when accesing the dir:
'ERROR 500 - Internal Server Error!'
This is the content of the nginx directiv field(for vhost):
# search for already compressed files
gzip_static on;
gzip on;
# some images have no mime type
default_type image/jpeg;
# 404 generated from php can be rather slow. Uncomment with care
error_page 404 /index.php;
# disallow access to version control directory, but return 404, not to disclose information
location /.git {
return 404;
}
# robots.txt is important for search engines
location /robots.txt {
access_log off;
}
# This is mostly based on Drupal's stock .htaccess
location ~* ^.+(\.(txt|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)|code-style\.pl|/Entries.*|/Repository|/Root|/Tag|/Template)$ {
return 404;
}
# serve imagecache files directly or redirect to drupal if they do not exist
location ~* imagecache {
access_log off;
expires 30d;
try_files $uri #drupal;
}
# Drupal 7 image stylef
location ~* image/generate {
access_log off;
expires 30d;
try_files $uri #drupal;
}
# serve static files directly
location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|swf|flv)$ {
access_log off;
log_not_found off;
expires 30d;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*)$ /index.php?q=$1;
break;
}
# This rewrites pages to be sent to PHP processing
location #drupal {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
location / {
try_files $uri #cache;
}
# This will try to see if we have a boost file in place. no harm done if this is not used
location #cache {
# queries, drupal cookies, or not GET methods, all require PHP processing.
if ($query_string ~ ".+") {
return 405;
}
if ($http_cookie ~ "DRUPAL_UID" ) {
return 405;
}
if ($request_method !~ ^(GET|HEAD)$ ) {
return 405;
}
error_page 405 = #drupal;
# Drupal uses 1978 - I am 4 years older than Dries :)
add_header Expires "Tue, 22 Sep 1974 08:00:00 GMT";
add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
try_files /cache/normal/$host/${uri}_.html /cache/mobile/$host/${uri}_.html /cache/perm/$host/${uri}_.css /cache/perm/$host/${uri}_.js /cache/$host/0$uri.html /cache/$host/0${uri}/index.html #drupal;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location /forum {
root /var/www/ashladan.net/web;
index index.php index.html index.htm;
location ~ ^/forum/(.+\.php)$ {
try_files $uri =404;
root /var/www/ashladan.net/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/forum/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/ashladan.net/web;
}
}
It may be standard PHP response for uncaught exception / fatal / syntax error with no output.
You can either turn on error logging in php.ini and check your log file, or just at the beginning of your index.php file in this directory add two lines:
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
So if it is PHP's fault you will get more details on this error.