Reputation: 993
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.
Upvotes: 0
Views: 1451
Reputation: 15468
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.
Upvotes: 1