cancel
Showing results for 
Search instead for 
Did you mean: 

Configuration problems with Nginx as reverse for Apache

SOLVED

Configuration problems with Nginx as reverse for Apache

I'm trying to setup Magento 2 on a Digital Ocean droplet with Ubuntu 16 as OS and where Nginx acts like a reverse proxy for Apache. The goal is to let Nginx handle the sessions and static files and hand off any other requests to Apache. I'm not great with Nginx configuration files that's probably why I'm facing this problem.

Magento is installed but there seems to be a problem with my Nginx configuration. Every Magento route returns the homepage (/contact /admin etc.).

1. Static files(/pub/static) that exist(not 404) are being returned just fine by Nginx

2. Static files the can't be found return the default Magento 404 page

3. All the other requests return the Magento homepage

 

I guess it has something to do with the following 2 location blocks in the Nginx configuration but after a few hours of testing and Googling I have no clue what the problem is:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}
location ~ (index|get|static|report|404|503|health_check)\.php$ {
    try_files $uri =404;
    proxy_buffering off;
    proxy_pass http://localhost:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

When I proxy_pass all the requests everything works fine and I can request any route with the desired effect:

#location / {
#   try_files $uri $uri/ /index.php$is_args$args;
#}
location / {
    try_files $uri =404;
    proxy_buffering off;
    proxy_pass http://localhost:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

But this also lets Apache grab the static files, something I do not want.

I have no idea what I'm doing wrong.

 

And when I edit the second location block as follows, I can request all the Magento routes when I using index.php (/index.php/contact for example).

    location ~ (index|get|static|report|404|503|health_check)\.php {
#       try_files $uri =404;
        proxy_buffering off;
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Client-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

I removed the $ sign at the top and commented out the try_files line.
Without /index.php everything still leads to the home page.
If I do not comment the try_files line all the requests including /index.php lead to the Magento 404 page.

 

If anyone could point me in the right direction that would be great.

1 REPLY

Re: Configuration problems with Nginx as reverse for Apache

And like always I found the solution 45 min later....

https://serverfault.com/a/755774/459171

location / {
    try_files $uri $uri/ @proxy;
}

location @proxy {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}

location ~ (index|get|static|report|404|503|health_check)\.php$ {
    try_files $uri =404;
    proxy_buffering off;
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}