How to setting two apps in one domain NGINX? - laravel

I have two apps.
Landing page (Nuxt.js)
Main Apps (Laravel Vue)
I got problem when I setting the NGINX. Everytime I enter the LaravelVue Apps, the assets always not found because its always access root in nuxtjs.
How to configure it?
My nginx look like this
server {
server_name mydomain.com;
#nuxt
location / {
root /var/www/html/nuxt;
proxy_pass http://localhost:8006;
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;
}
#laravel&vue
location /client {
root /var/www/html/laravue/public;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ /index.php?$query_string;
}
location /dashboard {
root /var/www/html/laravue/public;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
root /var/www/html/laravue/public;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Thanks

Related

Nginx different root for only mainpage

I am trying to configure nginx to set two roots for one domain. My folder structure looks like:
/var/www/
mainpage/
project/index.php
backend/
public/index.php
I need to have root url example.com uses project in folder mainpage and all other urls uses project in folder backend (this is laravel app).
I have started to try to access mainpage project by url example.com/mainpage, but I'm receiving 403 or "Input file not specified". My nginx config:
server {
server_name example.com;
root "/var/www/backend/public";
index index.html index.htm index.php;
charset utf-8;
location /mainpage/ {
alias /var/www/mainpage/project;
try_files $uri $uri/ /index.php$is_args$args;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
access_log off;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
This config return "403 forbidden". How should I configure nginx to work properly? And how to configure it to access mainpage for "/" url and other urls to use backend project
You can set up root directory inside each location e.g.:
location / {
try_files $uri $uri/ /index.php?$query_string;
root /your/root/directory;
}

Nuxt or Laravel does not get visitor's public ip

There is basic setup of Nuxt + Laravel (Apiato). For nuxt I'm using reverse proxy with nginx, and for Laravel is nginx also.
When I hit endpoint from Postman I get my public IP, but when it comes to Nuxt on live site, IP is 127.0.0.1.
I used request()->ip() to get the IP.
I already tried to add proxy_set_header but, either I did it wrong, or it isn't applied at all.
Both apps are using the same server.
Nuxt
server {
index index.html;
server_name example.com
location / {
proxy_pass http://localhost:8002;
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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-User-Agent $http_user_agent;
proxy_set_header X-Referer $http_referer;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Laravel
server {
index index.html;
server_name api.example.com;
root /var/www/api.example.com/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 unix:/run/php/php7.2-fpm.sock;
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;
}
error_log /var/log/nginx/api_error.log;
access_log /var/log/nginx/api_access.log;
}
Try this if you there is a proxy:
const ip = req.headers['x-forwarded-for'].split(',').pop() ||
req.connection.remoteAddress
else do following:
const ip = req.connection.remoteAddress

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.

Nginx configuration with Laravel API + vuejs

I have the application with frontend on vuejs which works through the laravel api on backend. And I use Laravel Passport for authorization.
When I start the application through the integrated laravel web server (artisan serve) on localhost:8000 it works good without any errors.
But when I moved it to my local web server I got some problems.
When I try to do the post request
axios.post('http://project.local/oauth/token', data)
I get the http error 405 (Method Not Allowed).
I excluded all routes from CSRF-token, added CORS headers and anyway it doesnt work. As I said on localhost:8000 it works well.
I didn't find any information about this problem and couldn't do it by myself. Does anyone know anything about it? Thanks in advance.
My nginx config
server {
listen 80;
root /home/me/project/frontend/dist;
index index.html index.php;
server_name project.local;
access_log off;
error_log /home/me/project/backend/error.log notice;
location / {
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /api/v1 {
root /home/me/project/backend/public;
rewrite ^/api/v1/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location /oauth/token {
root /home/me/project/backend/public;
rewrite ^/oauth/token/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
set $newurl $request_uri;
if ($newurl ~ ^/api/v1(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
if ($newurl ~ ^/oauth/token(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Maybe it's a cache! I just solved this problem and it turned out to be a cache cloudflare
cached all files as index.html

Nginx server laravel route directory 404 error on Windows

I'm getting a 404 every time I try to open a page other than the welcome page. I've read all the posts here on how to fix the problem, but none of the solutions worked for me.
I'm on Windows 10 and use WPN-XM for the Nginx server.
My laravel project is installed in C:/server/www/myapp
My configuration looks as follows:
server {
listen 127.0.0.1:80;
server_name localhost;
root www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_pool;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
# mitigate namespace conflict, see httpoxy.org
fastcgi_param HTTP_PROXY "";
include fastcgi_params;
}
I tried replacing root www with "C:/server/www/myapp/public" and a couple of other options.
I used try_files $uri $uri/ /index.php$is_args$args instead of try_files $uri $uri/ /index.php?$query_string
Any help will be appreciated.

Resources