Link 2 domains to 1 Laravel project via Nginx - laravel

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

Related

Laradock - Remove port from the url not working

I´ve been searching a lot and found a lot of answers but unfortunately non of the answers is working, my scenario is below:
In my project folder i have laradock and laravel folders. In the .env(inside laradock) i have:
NGINX_HOST_HTTP_PORT=8080
and this is because my 80 is ocuppied. and inside the nginx folder, in the default.conf i have:(note that the code below is pratically the default, i have putted here many things but it didn´t work)
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# For https
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name mam1.test;
root /var/www/laravelProject/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
}
Meanwhile i have to edit the hosts file on my mac and put: 127.0.0.1 laravelProject. If i acess laravelProject on my url it shows a message saying, it works which is not what i want, bu if i access with laravelProject:8080 it works great!. but how do i remove the port? i have tried many things, the last one was this but if it keeps NOT redirecting to the correct url.
How do i do this?
Regards

How to properly setup multitenant (subDomains) in a LEMP/Laravel 5.8 for local (DEV) environment?

I am running ubuntu 16.04 with PHP 7.1, NGINX and Laravel 5.8.
My goal is to create a multitenant app in my local (dev) environment.
The root url is myapp.com. I want to retrieve from the URL: account1.myapp.com, account2.myapp.com, ... and display back the string account1, or account2, ... .
After passing trough some posts:
Use /etc/hosts to direct wildcard domain name
How to put wildcard entry into /etc/hosts?
Multi-Tenant Laravel on Ubuntu
Wildcard in /etc/hosts file
I've learned that I should:
Install and setup dnsmasq /etc/dnsmasq.conf (address=/myapp.com/127.0.0.1)
Set the routes in Laravel to retrieve the subdomains
Set the hosts (127.0.0.1 myapp.com)
Set the /etc/nginx/sites-available/myapp.com.conf (server_name *.myapp.com myapp.com;)
After all that steps,
the url myapp.com is working good Route::get('/', 'HomeController#index');, but the url http://account1.myapp.com/, still not working (This site can’t be reached)
I can't figure out what I am doing wrong.
Some help?
That is the code I have so far:
ROUTE
Route::domain('{account}.myapp.com/')->group(function ($account) {
return $account;
});
Auth::routes();
Route::get('/', 'HomeController#index');
/etc/hosts
127.0.0.1 myapp.com
/etc/nginx/sites-available/myapp.com.conf
server {
listen 80;
server_name *.myapp.com myapp.com;
root /var/www/vhosts/myapp.com/public;
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/myapp.com-error.log error;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
client_max_body_size 1000m;
}
/etc/dnsmasq.conf
# Configuration file for dnsmasq.
#
address=/myapp.com/127.0.0.1
# Format is one option per line, legal options are the same ...
...

Rewrite laravel query parameters with nginx

I have a laravel web app that acts as a CMS and has several sites in one app. I access these sites with the following url:
my-site.com/site/1 - Leads to site 1
I want to rewrite NGINX so that i can point several top domains to their respective site in my CMS.
my-site-1.com -> my-site.com/site/1
How can this be achieved? All questions i can find wants to point a domain to a subfolder, i want to point it to a query parameter.
I use Larave forge + Digitalocean. This is my configuration file
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/before/*; #*/
server {
listen 80 default_server;
server_name default;
root /home/forge/default/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/server/*; #*/
location / {
try_files $uri $uri/ /index.php?$query_string;
if ($http_host ~ "^(www.)?my-site-1.com$"){
#I tried adding som conditional rules here
}
}
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;
fastcgi_connect_timeout 18000;
fastcgi_send_timeout 18000;
fastcgi_read_timeout 18000;
}
location ~ /\.ht {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/after/*; #*/

Nginx / Laravel config to redirect from non index.php route

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.

SSL laravel forge in default site

I have read this tutorial to set up SSL for my laravel site. I did not read everything (oops), and did not delete the "default" site. I have generated CSR from default site (but with example.com for domain). Now I have my certificate and i have installed it through laravel forge, but it does not work:
cannot connect to https://example.com (browser / curl)
http://example.com does not redirect to https
Nothing in /var/log/nginx/default-error.log
I restarted nginx
Here is my /etc/nginx/sites-available/default file content :
server {
listen 80;
#server_name default;
server_name example.com www.example.com;
#return 301 default$request_uri;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
#server_name default;
server_name example.com www.example.com;
root /home/forge/default/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/default/XXXXX/server.crt;
ssl_certificate_key /etc/nginx/ssl/default/XXXXX/server.key;
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/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
What can I do to make it work ?
Ok, it was a certificate problem. I tried nginx -t and there was an error, now I restarted all the process and everything is alright now.

Resources