LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-20-2024, 02:22 AM   #1
Jason.nix
Member
 
Registered: Feb 2023
Posts: 562

Rep: Reputation: 10
proxy_pass in Nginx based on file type


Hello,
I want to use Nginx for Node.js and PHP sites. My current configuration is:
Code:
upstream nodejs {
    server nodejs:3000;
}
​
server {
   listen 80;
   server_name default_server;
   error_log  /var/log/nginx/error.system-default.log;
   access_log /var/log/nginx/access.system-default.log;
   charset utf-8;
​
​
    root /usr/share/nginx/html;
    index index.html index.php index.js;
​
#    location ~ \.php$ {
#        fastcgi_pass    php-fpm:9000;
#        fastcgi_index   index.php;
#        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
#        add_header      script-filename $document_root$fastcgi_script_name always;
#        include         fastcgi_params;
#    }
​
​
location ~ \.js$ {
    proxy_pass http://nodejs;
    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 / {
     autoindex on;
     try_files $uri $uri/ $uri.html =404;
    }
}
When I use curl localhost:3000, then it works:
Code:
# curl localhost:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>
But when I use curl localhost, then it doesn't work:
Code:
# curl localhost
<html>
<head><title>Index of /</title></head>
<body>
<h1>Index of /</h1><hr><pre><a href="../">../</a>
<a href="bin/">bin/</a>                                               16-Apr-2024 17:07                   -
<a href="cfiles/">cfiles/</a>                                            20-Apr-2024 07:17                   -
<a href="node_modules/">node_modules/</a>                                      17-Apr-2024 13:18                   -
<a href="public/">public/</a>                                            16-Apr-2024 17:07                   -
<a href="routes/">routes/</a>                                            16-Apr-2024 17:07                   -
<a href="test/">test/</a>                                              16-Apr-2024 17:07                   -
<a href="views/">views/</a>                                             16-Apr-2024 17:07                   -
<a href="www/">www/</a>                                               16-Apr-2024 17:07                   -
<a href="CONTRIBUTING.md">CONTRIBUTING.md</a>                                    16-Apr-2024 17:07                2209
<a href="LICENSE">LICENSE</a>                                            16-Apr-2024 17:07                1076
<a href="README.md">README.md</a>                                          16-Apr-2024 17:07                 822
<a href="app.js">app.js</a>                                             16-Apr-2024 17:07                1074
<a href="docker-compose.yml">docker-compose.yml</a>                                 17-Apr-2024 19:14                1453
<a href="jenkinsfile">jenkinsfile</a>                                        16-Apr-2024 17:07                 962
<a href="package-lock.json">package-lock.json</a>                                  17-Apr-2024 17:37              148961
<a href="package.json">package.json</a>                                       17-Apr-2024 13:18                 396
</pre><hr></body>
</html>
As you can see it shows the contents of / webserver.
I know that if I change the location / as below, then the problem will be solved, but for other files like PHP, this will cause problems:
Code:
    location / {
     proxy_pass http://nodejs;
     autoindex on;
     try_files $uri $uri/ $uri.html =404;
    }
How can I solve it? Can I use multiple proxy_pass under the location /?

Thank you.

Last edited by Jason.nix; 04-20-2024 at 02:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
nginx proxy_pass node js app Boucane Linux - Server 2 04-05-2017 09:29 AM
LXer: Nginx+Varnish compared to Nginx LXer Syndicated Linux News 0 04-27-2011 02:30 PM
Nginx experts help me-problem when using Nginx php-fpm !!! HuMan-BiEnG Linux - Server 2 04-17-2011 02:30 PM
NGINX with PHP-FPM vis NGINX with Spawn-FCGI WhisperiN Linux - Server 1 03-15-2011 06:39 PM
Get file icon from extension, file type, or mime-type Guitarist88 Programming 3 04-21-2008 10:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration