Rewrite laravel query parameters with nginx - laravel

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/*; #*/

Related

Nginx config to allow custom domains in app

I have a multi-tenancy app hosted on DO. Users are able to create subdomains and also set their preferred custom domain. Need help in setting up the right Nginx config, here is the current one:
Requirements:
1- User setup for their required subdomain on app's domain (with SSL) - WORKING
2- User setup for their own custom domain - for this I have set up an A record to the server IP and below is the Nginx config - NOT WORKING
3- User custom domain is loaded with SSL - need help on how to structure this? Would an A record suffice?
#FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/wiki.pk/before/*;
server {
listen 80;
listen [::]:80;
server_name .one.com "";
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name .one.com "";
#server_name ~.;
#server_name _;
#server_name ~^(?.*)$;
server_tokens off;
root /home/forge/one.com/public;
#FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/one.com/1233982/server.crt;
ssl_certificate_key /etc/nginx/ssl/one.com/1233982/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA384;
ssl_prefer_server_ciphers off;
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;
#FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/one.com/server/*;
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/one.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
#FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/one.com/after/*;
Would appreciate all the help.

Link 2 domains to 1 Laravel project via Nginx

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 Forge 403 on server set up

I have set up and brand new server with forge and install a bitbucket repo
Forge nginx set up looks like this
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/before/*;
server {
listen 80 default_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;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!3DES';
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;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/server/*;
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.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/default/after/*;
On page load I'm getting a 403 error for the load of the site
Any ideas?
I followed the same process over and over. The project going in is a Laravel project , have run the normal composer and NPM installs.

nginx config for phpbb and laravel

I need an nginx config that will work for both Laravel and Phpbb.
I have used laravel forge to setup my digital ocean server, and it created this nginx config:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/before/*;
server {
listen 80;
listen [::]:80;
server_name djembefola.org;
root /home/forge/djembefola.org/public;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:$
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;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/server/*;
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/djembefola.org-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 ~ /\.(?!well-known).* {
deny all;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/djembefola.org/after/*;
The /public folder is where the front controller index.php of Laravel lives...
Also in the public folder, I have and install of phpbb at
- /public/board
I am upgrading the forum, and as such I need to rum the phpbb installer, which resides at:
localhost/board/install,
which then calls:
localhost/board/install/app.php/update
The above url is then giving a 404 error.
I have read elsewhere that this is because Nginx needs to be configured correctly in order to run the installer.
The sample Nginx config for phpbb is listed here.
So I need to merge these somehow, but so far my attempts have failed.
I tried adding :
location /board/ {
rewrite ^(.*)$ /app.php/$1 last;
}
to the existing laravel nginx file, but that fails. I am aware that I need to put it in the right place in the nginx config, but I fear I'm probably overlooking something else, as I am guessing a bit here...
Can anyone help please?
Based off the example config, you can nest required rules within a block wrap (just like they have done with the /install/ directory). eg:
location /board/ {
try_files $uri $uri/ #rewriteapp;
# Deny access to internal phpbb files.
location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|(?<!ext/)phpbb|store|vendor) {
deny all;
# deny was ignored before 0.8.40 for connections over IPv6.
# Use internal directive to prohibit access on older versions.
internal;
}
# Pass the php scripts to fastcgi server specified in upstream declaration.
location ~ \.php(/|$) {
# Unmodified fastcgi_params from nginx distribution.
include fastcgi_params;
# Necessary for php.
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
try_files $uri $uri/ /board/app.php$is_args$args;
fastcgi_pass php;
}
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
Note that #rewriteapp is outside the block wrap. Nginx will complain if you try to place it inside.
Just amend the paths to match your directory.

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