How to serve two laravel projects in nginx server with IP address? - laravel

I setup a laravel project on Ubuntu server with nginx, now I need to deploy a second project in the same server, if I don't have a domain name but IP address, how must I define the server_name for my second project? I tried with XX.XX.XX.XX/mysecondproject but I had an error when I reload nginx. I really appreciate if you can help me with this question. Here is my nginx file.
server {
listen 80;
listen [::]:80;
root /var/www/mysecondproject/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XX.XX.XX.XX/mysecondproject;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}

this approach worked for me. This way I have a first_project running at IP address (XX.XX.XX.XX) and a second application running at XX.XX.XX.XX/my_second_project. I used only the /etc/nginx/sites-available/default file for setup my server. I hope it helps somebody in the future.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/my_first_project/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name XX.XX.XX.XX //my IP Address;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /my_second_project {
alias /var/www/my_second_project/public;
try_files $uri $uri/ #my_second_project;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location #my_second_project {
rewrite /my_second_project/(.*)$ /my_second_project/index.php?/$1 last;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}

If you have a single ip addr for the instance like 10.0.1.20 and you want to serve both projects over the same ip address you can try the following configurations.
Seperated by Port
Your instance is lisiting on IP 10.0.1.20. Port 80 is the your App1 and Port 8080 is your App2.
#App1
server {
server_name 10.0.1.20;
listen 80;
.....
}
#App2
server {
server_name 10.0.1.20;
listen 8080;
.....
}
If you want to seperate your apps by location (/app1, /app2) and not by port.
Seperate by Location
#AppServer 1
server {
listen 8081;
......
}
#AppServer 2
server {
listen 8080;
......
}
#ProxyServer
server {
listen 80;
server_name 10.0.1.20;
location /app1 {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
......
}
location /app2 {
proxy_pass http://localhost:8081/;
proxy_set_header Host $host;
....
}
}

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

Docker Laravel Vuejs Nginx, issues loading assets

I can't get my Nginx config to work correctly.
I have a Laravel app, but the /admin uri is redirected by Nginx to a vuejs app.
The VueJS app index.html file is loading fine, but the assets are not, so the app isn't working.
The Laravel App and the VueJS apps are on two separate Docker Containers launched via docker-compose.
I have tried to play around with various combinations of "try_files" inside the "location /admin { }" section with no success so far. These adjustments have either resulted in 404s for everything or 500 Server Errors.
Here is my Nginx conf:
server {
listen 80;
listen [::]:80;
client_max_body_size 200M;
disable_symlinks off;
server_tokens off;
index index.php;
root /var/www/public;
location /admin {
root /var/www/public/dist;
proxy_pass http://admin:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass http://app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REALPATHTEST $realpath_root;
internal;
}
}
Here are screenshots showing that the index.html has loaded fine, but not the js & css assets:
HTML output in Chrome:
200 Result for page and 404 errors for assets:
Any help would be much appreciated.
In the end all I needed was this:
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://admin:8001;
}
So my final nginx config is:
server {
listen 80 default_server;
listen [::]:80 default_server;
client_max_body_size 200M;
disable_symlinks off;
server_tokens off;
index index.php;
root /var/www/public;
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://admin:8001;
}
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_pass app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REALPATHTEST $realpath_root;
internal;
}
}
I really hope this saves someone some time!!
If you are using vue-cli to setup your vue project, set baseUrl to /admin
Otherwise you will have to prefix your vue.js generated asset with /admin, or make nginx try files under vue project for every /css and /js requests.

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 app wildcard Digitalocean nginx

I have set up a Laravel app at Digitalocean (nginx).
It works fine when I go to site.com but when I go to www.site.com it won't load.
My question is how should I set up the DNS record at Digitalocean and how should i set the /etc/nginx/sites-available/default file:
www.site.com/path should redirect to site.com/path.
all the subdomain should refer to the main app site.
media.site.com should refer to other directory.
Thank you!
This is the file now:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/hzofe/public;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name media.site.com;
....
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name *.site.com;
....
}
Have you tries adding a cname record for DNS redirect?
Also, if you are using https you can make one cert for the alias domain and your regular with lets-encrypt.
This is a nodejs tutorial but it may help...
https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/#install-nginx

nginx / varnish / magento - 500 Error when changing port

I have installed varnish on nginx. I have some really big problems.
my default.vcl is:
backend default {
.host = "127.0.0.1";
.port = "81";
}
my website virtual server is:
server {
listen 80;
root /var/www/site.com.ro/public_html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name www.site.com.ro;
access_log /var/www/site.com.ro/logs/log.access;
error_log /var/www/site.com.ro/logs/log.error error;
location / {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler {
rewrite / /index.php;
}
location /blog {
alias /var/www/site.com.ro/public_html_blog/;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
location ~ ^/blog(.+\.php)$ { ### This location block was the solution
alias /var/www/site.com.ro/public_html_blog/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
# Make site accessible from http://localhost/
server_name site.com.ro;
rewrite ^(.*) http://www.site.com.ro$1 permanent;
}
and the varnish file is:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
when I try to change the listen port for the website to 81 I get a 500 error...
can anyone help me? I don't know what I did wrong.
Varnish is configured to listen to port 80, and connect to localhost:81. nginx is configured to listen to port 80. You didn't mention the intended flow, but I'm taking a wild shot and guessing:
client -> varnish:80 -> nginx:81.
Do you spot the problem now?
Hint:
server {
listen 80;
Oh, and make sure you have a real similar setup in a test machine (virtualbox - or something) that you use when you dabble with settings you're not familiar with. That will give you time to understand why something is not working, and will gain you invaluable experience so you don't have to mess up the production site(s).

Resources