LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   proxy_pass in Nginx based on file type (https://www.linuxquestions.org/questions/linux-server-73/proxy_pass-in-nginx-based-on-file-type-4175736246/)

Jason.nix 04-20-2024 02:22 AM

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.


All times are GMT -5. The time now is 03:12 AM.