Nginx reverse proxy Serving Node.js app static file










1















I have a Laravel application in which one route /onlineAds will take me to another application (an SPA one) built with Vue.Js as front and Node.js as Back. So I'm trying to use Nginx as a reverse proxy in order to serve my SpaApp's static files but without any success.



My conf is as follow:



/ => will be serverd from "C:/laragon/www/laravel_App/public/"
/onlineAds/(*) => will be serverd from "C:/laragon/www/VueNodeApp/dist/"
/api/(*) => will be proxied to nodeJs server


Here is what I tried to do with Nginx:



server 
listen 8080;
server_name domain.test *.domain.test;
root "C:/laragon/www/laravel_App/public/";

index index.html index.htm index.php;

location /
try_files $uri $uri/ /index.php$is_args$args;
autoindex on;


location ~* ^/onlineAds(.*)$
alias "C:/laragon/www/craiglist/dist/";
#try_files $uri $uri/ /index.html;


location ~* ^/api(.*)$
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_read_timeout 300;
proxy_pass http://localhost:8081;


location ~ .php$
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;



charset utf-8;

location = /favicon.ico access_log off; log_not_found off;
location = /robots.txt access_log off; log_not_found off;
location ~ /.ht
deny all;




What am I doing wrong?










share|improve this question
























  • Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

    – Abouhassane Abdelhamid
    Nov 14 '18 at 9:18











  • You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

    – Richard Smith
    Nov 14 '18 at 10:11











  • Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:23











  • You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

    – Richard Smith
    Nov 14 '18 at 10:45











  • I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:52
















1















I have a Laravel application in which one route /onlineAds will take me to another application (an SPA one) built with Vue.Js as front and Node.js as Back. So I'm trying to use Nginx as a reverse proxy in order to serve my SpaApp's static files but without any success.



My conf is as follow:



/ => will be serverd from "C:/laragon/www/laravel_App/public/"
/onlineAds/(*) => will be serverd from "C:/laragon/www/VueNodeApp/dist/"
/api/(*) => will be proxied to nodeJs server


Here is what I tried to do with Nginx:



server 
listen 8080;
server_name domain.test *.domain.test;
root "C:/laragon/www/laravel_App/public/";

index index.html index.htm index.php;

location /
try_files $uri $uri/ /index.php$is_args$args;
autoindex on;


location ~* ^/onlineAds(.*)$
alias "C:/laragon/www/craiglist/dist/";
#try_files $uri $uri/ /index.html;


location ~* ^/api(.*)$
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_read_timeout 300;
proxy_pass http://localhost:8081;


location ~ .php$
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;



charset utf-8;

location = /favicon.ico access_log off; log_not_found off;
location = /robots.txt access_log off; log_not_found off;
location ~ /.ht
deny all;




What am I doing wrong?










share|improve this question
























  • Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

    – Abouhassane Abdelhamid
    Nov 14 '18 at 9:18











  • You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

    – Richard Smith
    Nov 14 '18 at 10:11











  • Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:23











  • You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

    – Richard Smith
    Nov 14 '18 at 10:45











  • I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:52














1












1








1








I have a Laravel application in which one route /onlineAds will take me to another application (an SPA one) built with Vue.Js as front and Node.js as Back. So I'm trying to use Nginx as a reverse proxy in order to serve my SpaApp's static files but without any success.



My conf is as follow:



/ => will be serverd from "C:/laragon/www/laravel_App/public/"
/onlineAds/(*) => will be serverd from "C:/laragon/www/VueNodeApp/dist/"
/api/(*) => will be proxied to nodeJs server


Here is what I tried to do with Nginx:



server 
listen 8080;
server_name domain.test *.domain.test;
root "C:/laragon/www/laravel_App/public/";

index index.html index.htm index.php;

location /
try_files $uri $uri/ /index.php$is_args$args;
autoindex on;


location ~* ^/onlineAds(.*)$
alias "C:/laragon/www/craiglist/dist/";
#try_files $uri $uri/ /index.html;


location ~* ^/api(.*)$
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_read_timeout 300;
proxy_pass http://localhost:8081;


location ~ .php$
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;



charset utf-8;

location = /favicon.ico access_log off; log_not_found off;
location = /robots.txt access_log off; log_not_found off;
location ~ /.ht
deny all;




What am I doing wrong?










share|improve this question
















I have a Laravel application in which one route /onlineAds will take me to another application (an SPA one) built with Vue.Js as front and Node.js as Back. So I'm trying to use Nginx as a reverse proxy in order to serve my SpaApp's static files but without any success.



My conf is as follow:



/ => will be serverd from "C:/laragon/www/laravel_App/public/"
/onlineAds/(*) => will be serverd from "C:/laragon/www/VueNodeApp/dist/"
/api/(*) => will be proxied to nodeJs server


Here is what I tried to do with Nginx:



server 
listen 8080;
server_name domain.test *.domain.test;
root "C:/laragon/www/laravel_App/public/";

index index.html index.htm index.php;

location /
try_files $uri $uri/ /index.php$is_args$args;
autoindex on;


location ~* ^/onlineAds(.*)$
alias "C:/laragon/www/craiglist/dist/";
#try_files $uri $uri/ /index.html;


location ~* ^/api(.*)$
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_read_timeout 300;
proxy_pass http://localhost:8081;


location ~ .php$
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;



charset utf-8;

location = /favicon.ico access_log off; log_not_found off;
location = /robots.txt access_log off; log_not_found off;
location ~ /.ht
deny all;




What am I doing wrong?







node.js nginx vue.js single-page-application nginx-reverse-proxy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 11:41









Sayed Mohd Ali

1,5392520




1,5392520










asked Nov 14 '18 at 9:10









Abouhassane AbdelhamidAbouhassane Abdelhamid

238




238












  • Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

    – Abouhassane Abdelhamid
    Nov 14 '18 at 9:18











  • You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

    – Richard Smith
    Nov 14 '18 at 10:11











  • Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:23











  • You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

    – Richard Smith
    Nov 14 '18 at 10:45











  • I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:52


















  • Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

    – Abouhassane Abdelhamid
    Nov 14 '18 at 9:18











  • You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

    – Richard Smith
    Nov 14 '18 at 10:11











  • Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:23











  • You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

    – Richard Smith
    Nov 14 '18 at 10:45











  • I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

    – Abouhassane Abdelhamid
    Nov 14 '18 at 10:52

















Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

– Abouhassane Abdelhamid
Nov 14 '18 at 9:18





Note that for this configuration. it seems that laravel routing system catchs the /onlineAds and gives me a 404 error

– Abouhassane Abdelhamid
Nov 14 '18 at 9:18













You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

– Richard Smith
Nov 14 '18 at 10:11





You will get a 404 because your alias statement is wrong. An alias inside a regular expression location requires the full path to the file. Try: C:/laragon/www/craiglist/dist$1;

– Richard Smith
Nov 14 '18 at 10:11













Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

– Abouhassane Abdelhamid
Nov 14 '18 at 10:23





Thank you @RichardSmith, your suggestion has worked and now /onlineAds does give me the correct index file but the CSS and JS file do not load and givesme a 404. Any idea why?

– Abouhassane Abdelhamid
Nov 14 '18 at 10:23













You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

– Richard Smith
Nov 14 '18 at 10:45





You will see the GET requests for the CSS and JS files in the Nginx access log. That should tell you why Nginx cannot find the files.

– Richard Smith
Nov 14 '18 at 10:45













I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

– Abouhassane Abdelhamid
Nov 14 '18 at 10:52






I think that it tries to get the asset files from laravel's public folder and not from vue's dist folder!! "GET /onlineAds/ HTTP/1.1" 304 0 "-" "GET /css/app.9a1eae5e.css HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/app.1f025f79.js HTTP/1.1" 404 16216 "http://domain.test:8080/onlineAds/" "GET /js/about.d4a2497d.js HTTP/1.1" 404 18366 "http://domain.test:8080/onlineAds/"

– Abouhassane Abdelhamid
Nov 14 '18 at 10:52













1 Answer
1






active

oldest

votes


















0














An alias statement within a regular expression location requires the full path to the file. See this document for details.



For example:



location ~* ^/onlineAds(.*)$ 
alias "C:/laragon/www/craiglist/dist$1";
if (!-e $request_filename) rewrite ^ /onlineAds/index.html last;



The use of try_files with alias is avoided due to this issue. See this caution on the use of if.




Assuming that the URI /js/foo.js could be located in C:/laragon/www/laravel_App/public/js/foo.js or C:/laragon/www/craiglist/dist/js/foo.js, you could ask Nginx to try both locations using try_files with a common root directory.



For example:



location /js/ 
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;

location /css/
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;






share|improve this answer























  • Worked like a charm :D

    – Abouhassane Abdelhamid
    Nov 14 '18 at 15:02










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296509%2fnginx-reverse-proxy-serving-node-js-app-static-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














An alias statement within a regular expression location requires the full path to the file. See this document for details.



For example:



location ~* ^/onlineAds(.*)$ 
alias "C:/laragon/www/craiglist/dist$1";
if (!-e $request_filename) rewrite ^ /onlineAds/index.html last;



The use of try_files with alias is avoided due to this issue. See this caution on the use of if.




Assuming that the URI /js/foo.js could be located in C:/laragon/www/laravel_App/public/js/foo.js or C:/laragon/www/craiglist/dist/js/foo.js, you could ask Nginx to try both locations using try_files with a common root directory.



For example:



location /js/ 
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;

location /css/
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;






share|improve this answer























  • Worked like a charm :D

    – Abouhassane Abdelhamid
    Nov 14 '18 at 15:02















0














An alias statement within a regular expression location requires the full path to the file. See this document for details.



For example:



location ~* ^/onlineAds(.*)$ 
alias "C:/laragon/www/craiglist/dist$1";
if (!-e $request_filename) rewrite ^ /onlineAds/index.html last;



The use of try_files with alias is avoided due to this issue. See this caution on the use of if.




Assuming that the URI /js/foo.js could be located in C:/laragon/www/laravel_App/public/js/foo.js or C:/laragon/www/craiglist/dist/js/foo.js, you could ask Nginx to try both locations using try_files with a common root directory.



For example:



location /js/ 
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;

location /css/
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;






share|improve this answer























  • Worked like a charm :D

    – Abouhassane Abdelhamid
    Nov 14 '18 at 15:02













0












0








0







An alias statement within a regular expression location requires the full path to the file. See this document for details.



For example:



location ~* ^/onlineAds(.*)$ 
alias "C:/laragon/www/craiglist/dist$1";
if (!-e $request_filename) rewrite ^ /onlineAds/index.html last;



The use of try_files with alias is avoided due to this issue. See this caution on the use of if.




Assuming that the URI /js/foo.js could be located in C:/laragon/www/laravel_App/public/js/foo.js or C:/laragon/www/craiglist/dist/js/foo.js, you could ask Nginx to try both locations using try_files with a common root directory.



For example:



location /js/ 
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;

location /css/
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;






share|improve this answer













An alias statement within a regular expression location requires the full path to the file. See this document for details.



For example:



location ~* ^/onlineAds(.*)$ 
alias "C:/laragon/www/craiglist/dist$1";
if (!-e $request_filename) rewrite ^ /onlineAds/index.html last;



The use of try_files with alias is avoided due to this issue. See this caution on the use of if.




Assuming that the URI /js/foo.js could be located in C:/laragon/www/laravel_App/public/js/foo.js or C:/laragon/www/craiglist/dist/js/foo.js, you could ask Nginx to try both locations using try_files with a common root directory.



For example:



location /js/ 
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;

location /css/
root "C:/laragon/www";
try_files /laravel_App/public$uri /craiglist/dist$uri =404;







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 12:23









Richard SmithRichard Smith

21.1k42439




21.1k42439












  • Worked like a charm :D

    – Abouhassane Abdelhamid
    Nov 14 '18 at 15:02

















  • Worked like a charm :D

    – Abouhassane Abdelhamid
    Nov 14 '18 at 15:02
















Worked like a charm :D

– Abouhassane Abdelhamid
Nov 14 '18 at 15:02





Worked like a charm :D

– Abouhassane Abdelhamid
Nov 14 '18 at 15:02



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296509%2fnginx-reverse-proxy-serving-node-js-app-static-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo