Nginx does not serve/proxy path if it is missing ending slash

Discussion in 'Nginx' started by postcd, Aug 18, 2023.

  1. #1
    Hello dear Nginx users, being amateur user I wanted to ask You for your help.

    My Nginx configuration is properly serving a webpage when visiting:
    server.my.name
    my.name
    my.name/rooms
    my.name/room/abc
    my.name/r/abc/

    but not when i visit:
    my.name/r/abc
    (note that same path works when it has trailing slash)

    without slash, it redirects to
    http://127.3.2.1:4242/r/abc/ 
    Code (markup):
    my configuration file is:

    # cat /etc/nginx/nginx.conf
    
    #user nobody;
    worker_processes 1;
    
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;
    #pid logs/nginx.pid;
    
    events {
    worker_connections 1024;
    }
    
    http {
    access_log off;
    access_log /var/log/nginx/access.log;
    #access_log logs/access.log main;
    error_log on;
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    #tcp_nopush on;
    #keepalive_timeout 0;
    keepalive_timeout 65;
    #gzip on;
    # include /etc/nginx/sites-enabled/*;
    
    server {
    server_name 127.0.0.1 server.my.name;
    listen 80;
    listen [::]:80;
    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/my.name/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my.name/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    location / {
    root /var/www/html;
    }
    
    index index-server-var-www.html index.htm index.php
    try_files $uri $uri/ =404;
    
    location ~ /\.ht {
    deny all;
    }
    
    location = /favicon.ico {
    log_not_found off;
    access_log off;
    }
    }
    
    server {
    server_name https my.name www.my.name;
    client_max_body_size 10M;
    location /static/ {
    autoindex off;
    root /usr/lib/python3/dist-packages/sogs;
    # try_files $uri /404.html;
    # error_page 404 =200 /404.html;
    }
    location = /404.html {
    root /var/lib/session-open-group-server;
    }
    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://127.3.2.1:4242$request_uri;
    # try_files $uri $uri/ =404;
    # if ($request_uri = "?public_key=") { return 301 /; }
    }
    listen 80;
    listen [::]:80;
    listen 443 ssl; # managed by Certbot
    listen [::]:443 ssl;
    ssl_certificate /etc/letsencrypt/live/my.name/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my.name/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    # error_page 404 /404.html;
    # error_page 404 =200 /404.html;
    # try_files $uri /404.html;
    
    }
    }
    
    Code (markup):
    Since i think that i am not linking to any other .conf files, issue must be in above file, but where to correct it please?
     
    postcd, Aug 18, 2023 IP
  2. FlyPastaMonster

    FlyPastaMonster Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Hello.
    What does the log write?

    In any case, try adding a slash at the end of the proxy_pass line, before the semicolon
     
    FlyPastaMonster, Sep 18, 2023 IP
  3. postcd

    postcd Well-Known Member

    Messages:
    1,043
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #3
    This worked in one web browser for my.name/r/abc
    proxy_pass http://127.3.2.1:4242$request_uri/;
    Code (markup):
    but unfortunately it broken workings of the app that depends on the URL
    http://127.3.2.1:4242$request_uri; 
    Code (markup):
    .

    Access_log line was like: "GET /r/abc HTTP/1.1" 200 318 "-" "Mozilla/5.0
    Error log lines not found any around that time.
     
    postcd, Sep 18, 2023 IP
  4. hanoidesign

    hanoidesign Well-Known Member

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    108
    #4
    I think the redirect is caused by proxied server (perhaps it has behavior to redirect from 127.3.2.1:4242/r/abc to 127.3.2.1:4242/r/abc/).
    Try adding proxy_redirect to correct the header of server response like:

    proxy_redirect http://127.3.2.1:4242/ http://$host:$server_port/;
    Code (markup):
    Doc: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
     
    hanoidesign, Sep 20, 2023 IP
    postcd likes this.
  5. postcd

    postcd Well-Known Member

    Messages:
    1,043
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #5
    Yours mentioned proxy_redirect "rule" points to HTTP, yet my app is using HTTPs addresses and in this case it redirected
    https://my.name/r/abc
    Code (markup):
    to
    http://my.name:443/r/abc/
    Code (markup):
    showing:
    400 Bad Request The plain HTTP request was sent to HTTPS port
    Code (markup):
    After i have used HTTPs inside proxy_redirect, it WORKED (apparently solving this issue/thread), so thank you. Btw. I assume there is no way to make that htaccess redirect rule automatically detect which is the server primary protocol (http or https).
     
    postcd, Oct 3, 2023 IP
  6. hanoidesign

    hanoidesign Well-Known Member

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    108
    #6
    Nginx does have $scheme captured from original request URL. You could try:

    proxy_redirect http://127.3.2.1:4242/ $scheme://$host:$server_port/;
    Code (markup):
     
    hanoidesign, Oct 4, 2023 IP
    postcd likes this.