1

I am trying to do the following:

I have 2 docker php-fpm(no nginx) containers with the same directory structure but with different versions of code. Now I have another container with nginx in it.

When the requests goes to foo.com/ it should go to the first container and when the request goes to foo.com/v11/ it should go to the second container.

The problem is it doesn't work for the second scenario. ie when the request goes to foo.com/v11/, it simply won't go to the php-fpm server. relevant parts of the config file is below:

        location / {
                try_files $uri $uri/ /index.php?$query_string;
                location ~ \.php$ {
                        fastcgi_pass php_wbv1.0:5000;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                        fastcgi_param QUERY_STRING $query_string;
                        include fastcgi_params;
                }
        }

        location /v11/ {
                rewrite ^\/v11\/(.*)$ /$1 break;
                try_files $uri $uri/ /v11/index.php?$query_string;
                location ~ \.php$ {
                        fastcgi_pass php_wbv1.1:5001;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                        fastcgi_param QUERY_STRING $query_string;
                        include fastcgi_params;
                }
        }

Can somebody tell why its not working?

defiant
  • 121
  • 7
  • Where does the request go? And what's your docker-compose.yml (or how did you set up your containers)? – Michael Hampton Aug 05 '18 at 12:30
  • @MichaelHampton the requests are being handled directly by nginx, thats the issue. The first section is going to the php server fine, but the second location block is line it doesn't even exist. – defiant Aug 05 '18 at 13:20
  • So what happened to the request? What was the response? And what about my other question? – Michael Hampton Aug 05 '18 at 13:22
  • It shows 404 not found. I created the docker using docker file and docker build and create command. I was thinking since both containers are the same and the first container works when there is no path given in the location tag, it didn't matter how the docker was created. – defiant Aug 05 '18 at 13:30
  • Hmm. Are you sure nginx has started with the updated config? Try rebuilding the nginx container. – Michael Hampton Aug 05 '18 at 13:30
  • Ok I will try rebuilding the nginx container and let you know. So do you see any mistakes in my config? – defiant Aug 05 '18 at 13:41
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/81173/discussion-between-defiant-and-michael-hampton). – defiant Aug 05 '18 at 13:58
  • What do the nginx error and access logs show when you issue a request for /v11/ ? – Gerard H. Pille Aug 06 '18 at 06:36
  • @GerardH.Pille error log is empty, access log shows: `10.211.55.2 - - [06/Aug/2018:10:49:43 +0000] "POST /v11/timestamps/fetch HTTP/1.1" 405 182 "-" "curl/7.54.0"` – defiant Aug 06 '18 at 10:50
  • But 405 is not an nginx problem! "405 Method Not Allowed. The method received in the request-line is known by the origin server but not supported by the target resource." You should check the application listening on 5001. – Gerard H. Pille Aug 06 '18 at 11:10
  • What are you talking about? Of course its an nginx problem. The issue is how nginx is not forwarding the request without the /v11/ in its path. If the nginx starts to forward the requests without the /v11/ in the url, the issue is solved, thats what the question is all about. Anyway I have solved the issue with the help of fellows from the nginx mailing list. – defiant Aug 06 '18 at 15:22
  • Also if I were to include the path in the application listening on 5001, this issue would have been solved. Anyway my bad, my question didn't necessarily describe the issue I was facing correctly and I didn't say how I have to use the nginx config to make it work. Sorry. – defiant Aug 06 '18 at 15:27
  • No problem! I see in the solution that the rewrite is gone. Care to explain? – Gerard H. Pille Aug 07 '18 at 00:45
  • I couldn't make the rewrite work, instead I made nginx to look the index file in the right place. So changed the try files directive and hardcoded the path to the index file in `SCRIPT_FILENAME`. – defiant Aug 07 '18 at 01:25

2 Answers2

1

Your config is broken because of this line

#       }

so location /v11/ is nested within location /

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11
  • Sorry, thats not the issue, that was an error while copy pasting the code. Do you see any other issue? – defiant Aug 05 '18 at 12:21
1

I solved the issue with the help of fellows from the nginx mailing list. The issue was in try_files directive and the fastcgi_param SCRIPT_FILENAME. Here is the updated config:

location / {
            try_files $uri $uri/ /index.php?$query_string;
        location ~ \.php$ {
            fastcgi_index index.php;
            fastcgi_pass php_wbv1.0:5000;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param SCRIPT_FILENAME /home/apps/coreengine/public/index.php;
                    fastcgi_param QUERY_STRING $query_string;
                    include fastcgi_params;
        }
    }

    location /v11/ {
        try_files $uri $uri/ /v11/index.php?$query_string;
        location ~ \.php$ {
            fastcgi_index index.php;
            fastcgi_pass php_wbv1.1:5001;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param SCRIPT_FILENAME /home/apps/coreengine/public/index.php;
            fastcgi_param QUERY_STRING $query_string;
                    include fastcgi_params;
        }
    }
defiant
  • 121
  • 7