I have 2 folders dist and api_middleware in '/var/www/public_html/website.com/'
dist folder contains my frontend production code. This folder is symblink-ed from /home/user/frontend/dist
api_middleware folder contains codeignitor code, that we use as middleware for frontend to communicate with our erp. This folder is symblink-ed from /home/user/api_middleware.
I want to host both code with nginx. And this is the code that I came up with.
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
index index.php index.html index.htm index.nginx-debian.html;
location / {
root /var/www/public_html/website.com/dist;
try_files $uri $uri/ /index.html;
}
location /api_middleware {
root /var/www/public_html/website.com;
try_files $uri $uri/ /api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
navigating to website.com works. But the api calls with website.com\api_middleware\<PARAMS> is returning a 404
What am I doing wrong?
PS. Using ubuntu 18.4.
My Updated working code:
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/website.com.crt;
ssl_certificate_key /etc/ssl/website.com.key;
server_name website.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
root /var/www/public_html/website.com/dist;
index index.php index.html index.htm index.nginx-debian.html;
location /api_middleware {
alias /var/www/public_html/website.com/api_middleware/;
try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;
client_max_body_size 100M;
}
location ~ /api_middleware/.+\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_read_timeout 300;
}
}
The issue was that using multiple root was messing with nginx to access the directory location my second codebase.
I used alias to change the location to search for my 2nd codebase, while using root to set location of my 1st codebase.
Take a look at the line try_files $uri $uri/ /api_middleware/api_middleware/index.php?/$request_uri;. I have added 'api_middleware' twice here, even though the location to index.php file is /var/www/public_html/website.com/api_middleware/index.php . This is needed because of a very old bug in nginx.
finally, to process the php files, I changed the URI where to look for the php files to ~ /api_middleware/.+\.php$.
Reference:
NGINX try_files + alias directives
https://serverfault.com/questions/1035733/nginx-difference-between-root-and-alias
https://serverfault.com/questions/684523/nginx-multiple-roots/684605
Related
I have a Vue Cli 3 SPA application which makes api calls to a Laravel Backend. I've created a LEMP droplet on DigitalOcean, and I've cloned the two projects in the /var/www/html directory. api/ for the backend, web/ for the frontend. I've configured nginx root to web/dist/index.html. Now, how can I make api calls, since the root of the project is index.html?
I've searched a lot. I saw solutions where I must copy the dist folder's contents to api/public, and adjust nginx's root to be api/public/index.html. But that doesn't change the fact that I still can't make api calls, because index.php is never reached.
Could you please advice me how you do it? Should I create a subdomain?
Thanks!
UPDATE
I've tried this according to oshell's answer:
# For the vue app
server {
listen 80;
root /var/www/html/web/dist;
index index.html;
server_name XXX.XXX.XX.XXX # the ip addreess that I have
error_page 404 /;
location / {
try_files $uri $uri/ /index.html;
}
}
# for the laravel application
server {
listen 80;
root /var/www/html/api/public;
index index.php;
server_name XXX.XXX.XX.XXX/api;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Now whatever I open, it just goes to the vue application. If I try to make an api call to XXX.XXX.XX.XXX/api/something from the vue app, I've got 405 Method not allowed
You need to setup two separated servers for frontend and backend. You could make api reachable via api.example.com and frontend via example.com. The nginx config should look something like this:
#laravel.conf
server {
listen 80;
root /var/www/html/project_name/api;
index index.php index.html index.htm;
server_name api.example.com www.api.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
#vue.conf
server {
listen 80;
root /var/www/html/project_name/web/dist;
index index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.html;
}
}
You could also direct all traffic to you index.php and set it up so Route::any('/') returns the static page, including the static assets and all api routes are handled via Route::any('/api/foo').
The following configuration works for me on local environment - home directory on ubuntu.
Folder structure
example/dist - vue application
example/laravel - laravel api application
example/laravel/public - laravel public directory
example/laravel/public/images - laravel api images directory
Urls
example.lo - vue application
example.lo/api - laravel api
server {
# server name and logs
server_name example.lo;
access_log /var/log/nginx/example.lo_access.log;
error_log /var/log/nginx/example.lo_error.log;
root /home/username/example/laravel/public/;
index index.html index.php;
# location for vue app
location / {
root /home/username/example/dist/;
try_files $uri $uri/ /index.html;
}
# location for laravel api
location /api {
try_files $uri $uri/ /index.php$is_args$args;
}
# location for api images
location /images {
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Having some problems setting up the following setup with nginx:
example.com
example.com/gb/en
These are laravel sites and the gb/en is the first of more that will be added. Root works obvs but I can't get /gb/en to point at the /gb/en/public folder.
I have tried a bunch of things but this is my current conf file:
server {
listen 80;
listen [::]:80;
server_name www.example.com;
root /home/forge/www.example.com/public;
ssl_protocols TLSv1.2;
ssl_ciphers TEST;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/www.example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
You can see my location block in there which I have tried a number of different ways but to no avail.
Any assistance would be greatly appreciated.
EDIT:
So I've got one step closer once I realised that the sub folders in the url also get added to the root directory:
location /gb/en/ {
root /home/forge/www.example.com/public;
try_files $uri $uri/ /public/index.php?url=$uri;
}
So, my files are under /public/gb/en/public but the above resolves to /public/gb/en/index.php
How can I get /gb/en/ to resolve to public/gb/en/public/index.php?
Many thanks ;)
EDIT:
The idea came to me suddenly - use a symlink! I symlinked the /gb/en to the public folder in a separate folder and it worked a treat ;)
I would sugest to copy the try_files line into the /en/gb block. In that folder or site you are not specifying the extensions you would like to use as you are doing in the one that works.
Try this:
location /gb/en {
root /home/forge/www.example.com/gb/en/public;
try_files $uri $uri/ /index.php?$query_string;
}
However, if the content of /en/gb is related to the site, setting this up directly in Laravel might be a better option.
I used a symlink in the end to achieve this. I symlinked /gb/en to the public folder in another location and it did what I was after ;)
EDIT - I later found that this also had problems with sub pages but then found this example and it helped to achieve what I wanted to in the first place https://gist.github.com/noeldiaz/08a1211a7c47f21f7083
I'm trying to link another domain to my existing project
I am using Laravel 5.1, I know we only have one APP_URL in the .env.
Is there a way to do via Nginx level ?
cat /etc/nginx/sites-available/default
server {
listen 80 default_server;
server_name default;
root /home/forge/bheng/public;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/default-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
How would one go about configuring something like this?
put the two domains as alias in the server name config
server_name domain1.com www.domain1.com domain2.com www.domain2.com ;
You specify the same webroot for both domains. In the Laravel code, you use Domain groups or url('/') to check which domain you are on.
Your config might look like this:
server {
listen 80, 443;
listen [::]:80, [::]:443;
servername www.domain1.com www.domain2.com;
root /home/kyo/laravel/public/;
index index.php;
location / {
try_files $uri $uri/ =404;
}
}
I used something like this, a server name alias:
server_name www.app1 www.app2;
And then have DNS pointed to the same host. If you are working in your own linux box, change the /etc/hosts file:
sudo vim /etc/hosts
And add the new hosts:
127.0.0.1 www.app1
127.0.0.1 www.app2
Laravel 5.5 fresh
nginx version: nginx/1.10.3 (Ubuntu)
When I go to: http://<my-ip>/crm/ all is working great, I get the Laravel welcome page, all js and css are loading correctly.
When I go to http://<my-ip>/crm/register - I get 404 for css and js.
This is my conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
rewrite ^/((?!en)[a-z]*)/home$ /index.php?lang=$1&$args last;
rewrite ^/((?!en)[a-z]*)/sitemap.xml$ /sitemap.xml last;
rewrite /pigeon/(.*)$ /pigeon/index.php?/ last;
rewrite /crm/(.*)$ /crm/index.php?/ last;
if (!-f $request_filename){
set $rule_52 1$rule_52;
}
if ($rule_52 = "1"){
rewrite ^/(.*)(?<!php)$ /$1.php last;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pigeon {
alias /var/www/html/pigeon/public;
try_files $uri $uri/ #pigeon;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location /crm {
alias /var/www/html/crm/public;
try_files $uri $uri/ /index.php?$query_string;
location /crm.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
How do I fix this conf file to catch css and js files correctly?
Thanks
I had a similar issue. It seems allright what you have there in the server block.Although you might be better off if you add a proper server name.
Check this question and see if it helps. It has to do with the symlink to var/www/html for your crm, if you used one.
Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
And if not you could still use the online CDN's from bootstrap.Also in the question.
I've just deployed a newly built website running Laravel 5 and nginx and struggling to setup my nginx config to 301 redirect some old URLs - specifically those with .php extensions.
I'm looking to 301 redirect the below...
domain.com/foobar.php?id=123 -> domain.com/foobar/123
The default config assumes everything runs from index.php, so Laravel does not trigger on foobar.php.
I have tried adding the following rewrite rule, but it doesn't trigger (I assume because nginx isn't listening out for foobar.php requests?
rewrite ^/foobar.php(.*)$ /foobar/$1/ permanent;
Relevant snippet of my current nginx config below...
server {
listen 443 ssl;
server_name domain.com;
root /home/domain.com/public;
index index.html index.htm index.php;
charset utf-8;
if ($request_method = GET ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/domain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
This should work:
rewrite ^/foobar.php /foobar/$arg_id/? permanent;
You can get any argument like this $arg_{your_argument}. Just replace {your_argument} with the actual argument name.