Default Nginx set up in Laravel public folder - laravel

I had the default .htaccess set up for my Laravel app in /public folder (https://github.com/laravel/laravel/blob/9.x/public/.htaccess):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Now I've migrated to a new server environment with Nginx. I have managed to direct the requests to this /public directory, however I do not know how to replicate the working of this .htaccess on the new Nginx server, so the application works partially. For example calls to my api don't work (api.mysite.com)
UPDATE
OLD SET UP - APACHE SERVER
.htaccess in /wwwroot:
<IfModule mod_rewrite.c>
RewriteEngine on
# BACKOFFICE API
RewriteCond %{HTTP:Host} ^(?:api\.mysite\.com)?$
RewriteRule ^(.*) public/$1 [NC,L,NS]
# FRONTEND API
RewriteCond %{HTTP:Host} ^(?:src\.mysite\.com)?$
RewriteRule ^(.*) public/$1 [NC,L,NS]
# ANYOTHER TO DIST
RewriteCond %{REQUEST_URI} !^/dist
RewriteRule ^(.*)$ /dist/$1 [L]
.htaccess in /wwwroot/public was default Laravel .htaccess, the first one above.
NEW SET UP - NGINX (#van-Shatsky, I followed your advice in Nginx dynamic root from subdomain)
map $http_host $webroot {
src.mysite.com /home/site/wwwroot/public;
api.mysite.com /home/site/wwwroot/public;
default /home/site/wwwroot/dist;
}
server {
#proxy_cache cache;
#proxy_cache_valid 200 1s;
listen 8080;
listen [::]:8080;
server_name *.mysite.com;
root $webroot;
index index.php index.html;
}
With this configuration in Nginx, requests addressed to /dist (front-end application) are served correctly, but requests to api.mysite.com or src.mysite.com do not work. I hope I have clarified it.

This particular apache config could be rewritten for nginx the following way, for example:
location / {
try_files $uri $uri/ #maybe_rewrite;
}
location #maybe_rewrite {
# if we are here, $uri is not a physical file or directory
rewrite (.+)/$ $1 permanent;
# if previous rewrite rule isn't triggered, rewrite request to index.php
rewrite ^ /index.php last;
}
location ~ \.php$ {
try_files $uri /index.php$is_args$args;
include fastcgi_params;
fastcgi_param HTTP_AUTHORIZATION $http_authorization if_not_empty;
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass ...
}
However I don't know what are you meaning by "api calls not working" and how your current nginx config looks, so you'd better add your full nginx config to your question.

Related

Laravel files in public show blank

I have a problem when laravel site suddently not working (working well before this issue happened), it's shown 404 error all files in public folder. After I fixed via htaccess and/or views, 404 error stopped but no load data in any those public files.
Please help on this issue.
I'm trying to change htaccess and blade, stil not success.
Please advise.
My root htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1/$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
And public folder htaccess:
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
If you're getting a 404 error on all files in the public folder of your Laravel site, it's likely that the issue is related to the server configuration or permissions. Here are some things you can try to resolve the issue:
Check the file permissions: Make sure the files in the public folder
have the correct file permissions. The web server needs to have read
access to the files in order to serve them. You can use the chmod
command to change the file permissions.
Check the server configuration: Verify that the server configuration
is set up correctly to serve the files in the public folder. Check
the web server's configuration file, such as Apache's httpd.conf or
Nginx's nginx.conf, to ensure that the document root is set to the
correct directory.
Check the .htaccess file: If you're using an .htaccess file to
configure the server, make sure it's set up correctly. Make sure
that the RewriteBase directive is set to the correct path.
Check the file paths in your code: Double-check that the file paths
in your code are correct. If the file paths are incorrect, the
server will not be able to find the files it needs to serve.
Check for any server errors: Check the server logs for any error
messages. These logs can often provide useful information about
what's causing the issue.
Please be more clarified and detailed when next time you ask a question. That would be more helpful.
I need see you nginx conf, or reference my conf:
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
server {
listen 80;
server_name test.com;
index index.html index.htm index.php;
root D:/wwwroot/test/public;
autoindex on;
#301_START
#301_END
#HTTP_TO_HTTPS_START
#HTTP_TO_HTTPS_END
#SSL_START
#SSL_END
#REWRITE_START
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
#REWRITE_END
#PHP_START
include php/php-8.1.conf;
#PHP_END
#PHPMYADMIN_START
#PHPMYADMIN_END
#DENY_FILES_START
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 403;
}
#DENY_FILES_END
location ~ .+\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 1d;
error_log off;
access_log off;
}
location ~ .+\.(js|css)$
{
expires 1h;
error_log off;
access_log off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
access_log logs/test.com.access.log;
error_log logs/test.com.error.log;
}

Lumen home page working but not others on AWS

Im running a Lumen app on Elastic beanstalk
The index (home) page works but non of the others
Base url returns the index page. "Base url + /info" gives me 404
Iv checked the logs and can see nothing mentioned. Iv given the storage folder 777 permissions, also no joy. which is strange since the index page has been cached and is working.
The app works perfectly locally
Am I missing something obvious?
Thanks!
*****EDIT*****
More info:
public/htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
routes
$router->get('/', 'HomeController#index');
$router->get('info', 'HomeController#info');
HomeController
public function index() {
return view('index');
}
public function info() {
return view('info');
}
Both the index and Info pages extend the app layout page which is located in resources/views/layouts
On an Ngingx server, such as Elastic beanstalk is, the server config has to be edited as described here:
https://lumen.laravel.com/docs/5.1
In the server array add:
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Laravel 5.8 showing 404 Not Found errors

I change the index.php file path public to root. After adding a .htaccess file in the root path (laravel 5.7) every page working fine. But in laravel 5.8 when I click another page, it's showing 404 Not Found.
My .htaccess file is below.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d``
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The content of a Laravel .htaccess file should look like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Your webroot has to be set to the /public directory. Don't try to search for other solutions that move the index.php and .htaccess file from /public to the root directory of your Laravel application. There is absolutely no reason why you want to move the webroot from /public to /. index.php sits in /public for a reason. If your webhost does not offer the ability to move the web root to another directory switch your hoster.
It might be that you have created your folder as Example and trying to access it via example
ie
localhost/Example/public
yet accessing it like
localhost/example/public

zcart nginx configuration + htaccess conversion

Please help me to convert zcart application's htaccess rules to nginx configuration. Please see below htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Thanks
Assuming you are already configure your FastCGI part of config, lets try:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
can be converted to nginx configuration as
location ~ ^(.+)/$ {
if (!-d $request_filename) {
return 301 $1;
}
# else this is a directory, will be processed via index file in this directory by default
}
or (the same result)
location ~ ^(.+)/$ {
if (!-d $request_filename) {
rewrite ^(.*)/$ $1 permanent;
}
# else this is a directory, will be processed via index file in this directory by default
}
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
will be processed by a single try_files directive in the main location block:
location / {
try_files $uri $uri/ /index.php;
}
And, finally,
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
requires no additional config in nginx.conf, because $_SERVER[HTTP_AUTHORIZATION] variable will be appended to $_SERVER array automatically when HTTP Authorization header is set (if your php-fpm configuration correct).

Redirecting URL without www to www

I need your help. I want to test if the URL is entered without www
like example.com it should be forwarded to www.example.com.
Try this mod_rewrite rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [NC,L,R=301]
If you are using nginx, then add this line to nginx config:
server {
listen 80;
server_name yourdomain.com;
rewrite ^/(.*) http://www.yourdomain.com/$1 permanent;
}

Resources