Location directive of NGINX doesn't works

6,762

I wonder your site are using relative paths, if it's not working try this:

server {
    listen       8080;
    server_name  mochawesome;

    root   html;
    index  index.html;

    location /xxx {
        proxy_pass http://localhost:8080/mochawesome-report/mochawesome.html;
        sub_filter '<head>' '<head>\n<base href="/mochawesome-report/">';
    }

    location / {
        root /usr/share/nginx/html;
    }
}

It will change the base tag of mochawesome.html, making all requests from the proxied source to work.

Share:
6,762

Related videos on Youtube

André Mendes
Author by

André Mendes

Updated on September 18, 2022

Comments

  • André Mendes
    André Mendes over 1 year

    I'm trying to create a web server to share my test results reports using NGINX.

    For this, i'm currently using this docker image.

    Inside /usr/share/nginx/html/ of this container, i have:

    -mochawesome-report

    --assets

    --mochawesome.html

    -Dockerfile

    -nginx.conf

    My intention is to browse to localhost:8080/api-results/ and see mochawesome.html.

    server {
    
        listen 80;
        root /usr/share/nginx/html;
    
        location /api-results/ {
            root /mochawesome-report;
            index mochawesome.html;
        }
    
    
    }
    

    But unfortunately, it doesn't works:

    172.17.0.1 - - [16/May/2018:14:21:24 +0000] "GET /api-results/ HTTP/1.1" 404 572 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36" "-"
    2018/05/16 14:21:24 [error] 7#7: *1 "/usr/share/nginx/html/api-results/index.html" is not found (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /api-results/ HTTP/1.1", host: "localhost:8080"
    

    Also, this is my Dockerfile:

    FROM nginx:alpine
    EXPOSE 80
    WORKDIR /usr/share/nginx/html
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY . /usr/share/nginx/html/
    CMD ["/usr/sbin/nginx"]
    
    • Admin
      Admin about 6 years
      can you share your Dockerfile it may shed more light on your problem?
    • Admin
      Admin about 6 years
      @Const Sure. I've updated my questions.
  • André Mendes
    André Mendes about 6 years
    It doesn't works. Pretty much the same problem described above :/ ("/usr/share/nginx/html/mochawesome-report/index.html" is not found)
  • Admin
    Admin about 6 years
    Did you recreate exactly same steps? Since this was posted from working system. Make sure you don't have web server on host interfering.
  • Admin
    Admin about 6 years
    by "/usr/share/nginx/html/mochawesome-report/index.html" is not found seems like it expects it in html folder not report-api sub-folder. Either adjust root path or move it to that folder and it should work.