Docker Laravel Vuejs Nginx, issues loading assets - laravel

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.

Related

How to setting two apps in one domain NGINX?

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

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

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;
....
}
}

Deploying Vuejs and Laravel App on SSL nginx

I have working Vuejs + Laravel App behing NGINX proxy. All my Laravel request are rest based and at www./app/$. It was working fine at separte ports. But when i switched to SSL. My frontend (Vuejs) still working fine but whenever I tried to send post request to backend (laravel), I receive [error] 8#8: *80 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream.
In 443 ssl block I am diverting all rest api calls to phpfpm but its not working somehow. What is recommended way of deploying these vuejs+ laravel on SSL nginx.
In the past, I've tried moving phpfpm in the ssl server block. I tried adding http on in fastcgi setting. After all changes, Following code:
Following is my Conf file.
server {
listen 81; # backend at laravel
index index.php index.html;
root /var/www/public;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
server {
listen 88; # frontend vuejs build app
index index.html;
server_name app.lookahead.com www.app.lookahead.com;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/front;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name app.lookahead.com;
ssl_certificate /etc/certs/app.lookahead.com/cert.crt;
ssl_certificate_key /etc/certs/app.lookahead.com/cert.key;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8088;
proxy_read_timeout 90;
return 301 https://www.app.lookahead.com$request_uri;
}
location ^~ /api {
root /var/www/public; #backend api at this root
try_files $uri $uri/ /api/index.php$is_args$args;
location ~* \.php(/|$) {
fastcgi_pass app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
##https://www.app.lookahead.com
server {
server_name www.app.lookahead.com;
listen 443;
ssl_certificate /etc/certs/app.lookahead.com/cert.crt;
ssl_certificate_key /etc/certs/app.lookahead.com/cert.key;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
root /var/www/front; #frontend static vuejs files at this root
index index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location ^~ /api {
root /var/www/public; # backend api is at this root
try_files $uri $uri/ /api/index.php$is_args$args;
location ~* \.php(/|$) {
fastcgi_pass app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# nginx serving frontend
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
}

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

Firefox redirecting Nginx rewrite

Firefox is the only browser I am having issues with. I have found similar issues, but no solutions seem to work.
When I visit http://example.com nginx rewrites it as http://www.example.com.
I did this because the site used ssl sitewide, where now that has remains on the initial server using a subdomain, so is https://subdomain.example.com. Search engines, old bookmarks, and other old links attempted to take the user to https://example.com.
In all Browsers this works like a charm, except in firefox.
The Problem: Firefox takes the users request of http://example.com and forwards them to https://subdomain.example.com.
And then from the search engine link that reads https://example.com, an SSL error is raised because it's trying to read subomain.example's.
I'm getting confused and now it's 430 in the morning. Does someone have any clues here?
Here's my nginx conf:
upstream thin_server {
server 0.0.0.0:8080 fail_timeout=0;
}
server {
listen 80 default;
listen 443 ssl;
ssl off;
root /home/example/public;
server_name example.com www.example.com;
ssl_certificate /etc/nginx/ssl/www.example.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
index index.htm index.html;
if ($host = 'example.com') {
rewrite ^/(.*)$ http://www.example.com/$1;
}
location / {
try_files $uri/index.html $uri.html $uri #app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|avi)$ {
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://thin_server;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
UPDATE Just started working randomly a couple of days later
I had the a similar issue, Chrome was working fine, IE and firefox did not working with the http to https redirect.
I was searching for a day, build various configurations but nothing helped.
By accident I checked my firewall (ufw status) and realized that port 80 was not open, only 443.
After allowing port 80 it worked.
Here is my nginx config which is working ( I know it is not optimized )
# Redirect http to https
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain.tl www.domain.tl *.domain.tl;
return 301 https://www.domain.tl$request_uri;
}
#HTTPS config for SSL with certificate
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.domain.tl www.domain.tl;
#Limited Cipers to avoid MD5 etc attacks
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
#Limit to TLSv1.2 for security
ssl_protocols TLSv1.2;
#Chained certificate to make sure the intermediate is in
ssl_certificate /etc/nginx/ssl/certificate.chain.crt;
ssl_certificate_key /etc/nginx/ssl/certificat_key.key;
#PHP, Wordpress etc config
root /var/www/html;
index index.php index.html index.htm;
# unless the request is for a valid file, send to bootstrap
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
#Rewrite rule fuer Wordpress
try_files $uri $uri/ /index.php?$args;
}
# PHP7 specific
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# OLD CONFIG for php5
# location ~ \.php$ {
# try_files $uri =404;
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}
}

Resources