502 Bad Gateway when accessing Nuxt deployment
shreyas-satish
HOBBYOP

8 months ago

The project deployed successfully, however when we create a deployment url it shows 502 bad gateway.

What we have tried

  • create a port env in railway env's and add 8000

  • tried ports 8080, 8000, 3000 etc

our nuxt config file

// nuxt.config.ts
export default defineNuxtConfig({
  // Essential modules that your application needs
  modules: [
    "@nuxt/content", // Handles markdown content
    "@nuxt/ui", // Provides UI components
    "nuxt-monaco-editor", // Code editor
    "@pinia/nuxt", // State management
  ],

  css: ["~/assets/css/content.css", "~/assets/css/editor.css"],

  // Components configuration
  components: {
    dirs: [
      {
        path: "~/components",
        global: true,
      },
      {
        path: "~/components/content",
        global: true,
        prefix: "Content",
      },
    ],
  },

  content: {
    navigation: {
      fields: ["navigation"],
    },
    highlight: {
      theme: "github-dark",
      preload: ["vue", "javascript", "typescript"],
    },
    markdown: {
      remarkPlugins: [],
      rehypePlugins: [],
      tags: {
        p: "p",
        a: "a",
        blockquote: "blockquote",
        "code-inline": "code",
        code: "pre",
        em: "em",
        h1: "h1",
        h2: "h2",
        h3: "h3",
        h4: "h4",
        h5: "h5",
        h6: "h6",
        hr: "hr",
        img: "img",
        ul: "ul",
        ol: "ol",
        li: "li",
        strong: "strong",
        table: "table",
        thead: "thead",
        tbody: "tbody",
        td: "td",
        th: "th",
        tr: "tr",
      },
      componentType: true,
      mdc: true,
      toc: {
        depth: 3,
        searchDepth: 3,
      },
      anchorLinks: false,
    },
    components: {
      global: true,
      dirs: ["~/components/content"],
    },
  },

  // Configuration that changes based on environment
  runtimeConfig: {
    github: {
      clientId: process.env.NUXT_GITHUB_CLIENT_ID,
      clientSecret: process.env.NUXT_GITHUB_CLIENT_SECRET,
    },
    public: {
      githubClientId: process.env.NUXT_PUBLIC_GITHUB_CLIENT_ID,
      siteUrl: process.env.NUXT_PUBLIC_SITE_URL
        ? `https://${process.env.NUXT_PUBLIC_SITE_URL}`
        : process.env.NUXT_PUBLIC_SITE_URL || "http://localhost:3000",
      githubOwner: "XYZ",
      githubRepo: "XYZ",
    },
  },

  monacoEditor: {
    // These are default values:
    locale: "en",
    componentName: {
      codeEditor: "MonacoEditor",
      diffEditor: "MonacoDiffEditor",
    },
  },
  
  // Configure Nitro server
  nitro: {
    externals: {
      // Include minio in the server bundle
      inline: ['minio']
    },
    preset: "node", // Explicitly set for Railway
    prerender: {
      crawlLinks: false,
    },
  },

  // Development server configuration
  devServer: {
    port: 3000,
  },

  compatibilityDate: "2025-01-07",

  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "~/assets/scss/core/_variables.scss";
          `,
          quietDeps: true,
        },
      },
    },
  },

  // Add this to suppress TypeScript errors (fix build erros)
  typescript: {
    strict: false,
  },
});

Nixpacks.toml file

[phases.install]

cmds = ['npm ci --legacy-peer-deps']

[phases.build]

cmds = ['npm run build']

[start]

cmd = 'node .output/server/index.mjs'

and
package.json

  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "engines": {
    "node": "18.x"
  },
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare",
    "preinstall": "mkdir -p .build-info && echo $(date) > .build-info/build-timestamp",
    "start": "node .output/server/index.mjs"
  },

How to get a proper deployment url and Fix 502 bad gateway error?

Solved

3 Replies

chandrika
EMPLOYEE

8 months ago

Hey Shreyas, could you please check out our Nuxt guide and see if it helps https://docs.railway.com/guides/nuxt ?

Could you also ensure your application is binding to 0.0.0.0 and listening on the port specified by the PORT environment variable that Railway injects into your application. Railway assigns a dynamic port to your service, and your application needs to listen on that specific port.

You might need to modify your Nuxt configuration to respect the PORT variable provided by Railway. Instead of hardcoding a port in your nuxt.config.ts or elsewhere in your application, using the process.env.PORT variable like so could help:

// nuxt.config.ts
export default defineNuxtConfig({
  server: {
    host: '0.0.0.0', // Default: localhost
    port: process.env.PORT || 3000 // Default: 3000
  },
  // other configurations...
});

Could you make sure that your start script in package.json uses this configuration? Your existing start script looks good as it is already pointing to node .output/server/index.mjs, which should follow the configurations defined in your Nuxt setup.

Once you've made these changes, redeploy and check the detailed logs to see if there are any other underlying issues. Our docs on Application Failed to Respond are also really helpful in such cases. Hope this helps and let me know Let me know if you need any further assistance!


Status changed to Awaiting User Response Railway 9 months ago


shreyas-satish
HOBBYOP

8 months ago

Hey, we have updated our nuxt configuration and added the port env, and we are following the railway guide as well, the error is still present
Deployment url shows 502 error (there are no errors in build/deployment process)

// nuxt.config.ts
export default defineNuxtConfig({
  // Essential modules that your application needs
  modules: [
    "@nuxt/content", // Handles markdown content
    "@nuxt/ui", // Provides UI components
    "nuxt-monaco-editor", // Code editor
    "@pinia/nuxt", // State management
  ],

  // Add this server configuration for Railway
  server: {
    host: '0.0.0.0', // Listen on all network interfaces
    port: process.env.PORT || 3000 // Use Railway's PORT or default to 3000
  },

  css: ["~/assets/css/content.css", "~/assets/css/editor.css"],

  // Components configuration
  components: {
    dirs: [
      {
        path: "~/components",
        global: true,
      },
      {
        path: "~/components/content",
        global: true,
        prefix: "Content",
      },
    ],
  },

  content: {
    navigation: {
      fields: ["navigation"],
    },
    highlight: {
      theme: "github-dark",
      preload: ["vue", "javascript", "typescript"],
    },
    markdown: {
      remarkPlugins: [],
      rehypePlugins: [],
      tags: {
        p: "p",
        a: "a",
        blockquote: "blockquote",
        "code-inline": "code",
        code: "pre",
        em: "em",
        h1: "h1",
        h2: "h2",
        h3: "h3",
        h4: "h4",
        h5: "h5",
        h6: "h6",
        hr: "hr",
        img: "img",
        ul: "ul",
        ol: "ol",
        li: "li",
        strong: "strong",
        table: "table",
        thead: "thead",
        tbody: "tbody",
        td: "td",
        th: "th",
        tr: "tr",
      },
      componentType: true,
      mdc: true,
      toc: {
        depth: 3,
        searchDepth: 3,
      },
      anchorLinks: false,
    },
    components: {
      global: true,
      dirs: ["~/components/content"],
    },
  },

  // Configuration that changes based on environment
  runtimeConfig: {
    github: {
      clientId: process.env.NUXT_GITHUB_CLIENT_ID,
      clientSecret: process.env.NUXT_GITHUB_CLIENT_SECRET,
    },
    public: {
      githubClientId: process.env.NUXT_PUBLIC_GITHUB_CLIENT_ID,
      siteUrl: process.env.NUXT_PUBLIC_SITE_URL
        ? `https://${process.env.NUXT_PUBLIC_SITE_URL}`
        : process.env.NUXT_PUBLIC_SITE_URL || "http://localhost:3000",
      githubOwner: "tiresomefanatic",
      githubRepo: "EchoProdTest",
    },
  },

  monacoEditor: {
    // These are default values:
    locale: "en",
    componentName: {
      codeEditor: "MonacoEditor",
      diffEditor: "MonacoDiffEditor",
    },
  },
  
  // Configure Nitro server
  nitro: {
    externals: {
      // Include minio in the server bundle
      inline: ['minio']
    },
    preset: "node", // Explicitly set for Railway
    prerender: {
      crawlLinks: false,
    },
  },

  // Development server configuration
  devServer: {
    port: 3000,
  },

  compatibilityDate: "2025-01-07",

  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "~/assets/scss/core/_variables.scss";
          `,
          quietDeps: true,
        },
      },
    },
  },

  // Add this to suppress TypeScript errors (fix build erros)
  typescript: {
    strict: false,
  },
});

Status changed to Awaiting Railway Response Railway 9 months ago


chandrika
EMPLOYEE

8 months ago

Hi there, could you please check our docs on Application Failed to Respond and the nuxt guide: Deploy a Nuxt App to see if it helps configure your app correctly?


Status changed to Awaiting User Response Railway 9 months ago


Railway
BOT

4 months ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 4 months ago


Loading...