Question

configuration for nginx for static files in Nodejs

Hi all ,

I am a beginner with Node.

I followed the guide https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04 and everything is working fine but I am having issues with static folder where I store css picture and other files.

The configuration for the website nginx ( copied for the guide above)

server {
        listen 80 ;
        listen [::]:80 ;

        root /var/www/websitefolder/html;

       
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {

        proxy_pass http://localhost:3000;
        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;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 try_files $uri $uri/ =404;

I copy/paste below the node.js I am trying to run

app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
//app.use("/public", express.static("public"));
//app.use("/public", express.static("./public"));

app.use(express.static("public")); 

app.get("/", function(req, res) {


var today = new Date();

var options = {
  weekday: "long",
  day: "numeric",
  month: "long",
}
....

error:

Failed to load resource: the server responded with a status of 404 (Not Found)
styles.css

Thanks, Angelo


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there,

I would usually use the following Nginx configuration to have the static files working:

upstream backend {
server localhost:3000;
}

server {
    listen 80;
    server_name example.com www.example.com;


    root /var/www/your_project;

    location / {
       try_files $uri @backend;
    }

    location @backend {
         proxy_pass http://backend;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         # Following is necessary for Websocket support
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
     }
}

Regards, Bobby