nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.37k stars 323 forks source link

Can Unit run NodeJS Nuxt applications? #1013

Closed rommanio closed 7 months ago

rommanio commented 10 months ago

We have infrastructure with many PHP applications. A part of these applications we're running in Nginx Unit. We have external developers who develop Nuxt application. We want to unify our infrastructure and use Unit everywhere. But when we trying to build Nuxt application and run it in Unit (as documented here, in Unit documentation), curl stucks on Unit port, but on Node port, 3000, curl works normally.

tippexs commented 10 months ago

Good morning!

Do you have an example application so we can reproduce the issue?

rommanio commented 10 months ago

Yes, we can provide it. https://github.com/rommanio/sample-nuxt-unit-app You can build and run it by docker compose -f docker/docker-compose.yml build && docker compose -f docker/docker-compose.yml up.

tippexs commented 10 months ago

@javorszky please give it a try to check whats the issue. Feel free to invite other to join the research.

javorszky commented 10 months ago

I've been debugging for the past hour and a bit. I have an idea where / why it hangs, but haven't found the concrete issue yet. I'll keep everyone updated once I either get stuck and need more answers or found a resolution :)

javorszky commented 10 months ago

@rommanio we found a possible cause for this. Testing whether that's causing it and trying to fix it.

rommanio commented 10 months ago

@rommanio we found a possible cause for this. Testing whether that's causing it and trying to fix it.

Good news. Thank you! :)

javorszky commented 10 months ago

@rommanio I've opened a PR to fix this issue that I tested locally: https://github.com/nginx/unit/pull/1020

Here's how you can test the same changesets on your end:

  1. copy unit-http into your sample repository's docker folder. I had that local copy as ./docker/unit-http. This is so the local files can be copied into the dockerfile
  2. modify the dockerfile so it looks like this:

    FROM unit:1.31.1-node20
    
    #COPY --chown=unit . /var/www/
    
    RUN mkdir -p /builder
    COPY . /builder
    COPY docker/unit-http /lib/unit-http
    WORKDIR /builder
    RUN npm i --progress=false
    RUN npm run build
    RUN mkdir -p build && cp -r .output/server build/
    RUN npm run generate
    RUN cp -r .output/public build/
    RUN cp -r build /var/www/
    RUN chown -R unit:unit /var/www
    
    RUN chmod +x /var/www/server/index.mjs
    
    WORKDIR /var/www/server

    notable changes:

    • removed all the npm links
    • copied the local copy of unit-http from the host into /lib/ folder inside the dockerfile
    • removed copying the unit.json file
  3. change the unit.json file's arguments part to this:
            "arguments": [
                "node",
                "--loader",
                "/lib/unit-http/loader.mjs",
                "--require",
                "/lib/unit-http/loader",
                "index.mjs"
            ],

    notable changes:

    • replaced the require and loader paths from specifying only the package name to its path on the disk: /lib/...
  4. change the docker-compose.yml to add a volume mount on it:
        volumes:
          - ./unit.json:/docker-entrypoint.d/config.json

    this way the image doesn't need to be rebuilt if we're only making configuration changes

  5. lastly change the local copy of the ./docker/unit-http/loader.js and ./docker/unit-http/loader.mjs files so they add the new lines that are in the pull request.

That fixed the issue for me.

andrey-zelenkov commented 8 months ago

Could you please also include short description in docs/changes.xml (for release purposes), since this change is visible for users.

Something like:

diff --git a/docs/changes.xml b/docs/changes.xml
--- a/docs/changes.xml
+++ b/docs/changes.xml
@@ -40,6 +40,13 @@ with Next.js.
 </para>
 </change>

+<change type="bugfix">
+<para>
+Node.js module name rules, notable for Node.js Nuxt applications.
+</para>
+</change>
+
+
 </changes>