Nginx over apache gives 502 Bad Gateway

9,323

I fixed this by changing proxy_pass from https://... to http://....

Share:
9,323

Related videos on Youtube

Mark Topper
Author by

Mark Topper

I am a 23 years old web-developer from Denmark.

Updated on September 18, 2022

Comments

  • Mark Topper
    Mark Topper over 1 year

    I'm trying to run Nginx over Apache.

    My configuration is that I set Apache to listen to port 8080 instead of 80.

    Then I set Nginx to proxy all requests to the same domain over port 8080:

    upstream app {
        server example.com:8080;
    }
    
    server {
        listen 80;
        server_name example.com;
    
        ssl_protocols TLSv1.2;
    
        charset utf-8;
    
        index index.html index.htm index.php;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        access_log off;
        error_log  /var/log/nginx/example.com-error.log error;
    
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
    
            proxy_pass https://app/;
            proxy_redirect off;
    
            # Handle Web Socket connections
            proxy_http_version 1.0;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    

    Then I setup the Apache configuration, which seems to work just fine, cause I can access my site at http://example.com:8080 without problems.

    <VirtualHost *>
        DocumentRoot "/home/forge/example.com"
        ServerName example.com
        ServerAlias www.example.com
        CustomLog /var/log/httpd/example_com_access.log common
        ErrorLog /var/log/httpd/example_com_error.log
    
            <Directory /home/forge/example.com/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
            </Directory>
    
    </VirtualHost>
    

    But when trying to go to http://example.com/ I get 502 Bad Gateway - nginx/1.8.0.

    Any ideas how to fix this?

    The reason I need this is that I have one web server which holds a lot of websites using Nginx, but I need some (only some of them) of the sites to run Apache rules instead of Nginx rules.

    I am running Ubuntu 14.04.

    EDIT: Here is the log from /var/log/nginx/error.log:

    2015/11/06 11:05:27 [emerg] 18176#0: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/thehostboy.com:71
    2015/11/06 11:07:49 [emerg] 18564#0: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/thehostboy.com:71
    2015/11/06 11:23:25 [notice] 21045#0: signal process started
    2015/11/06 11:23:25 [alert] 20875#0: *60679 open socket #4 left in connection 9
    2015/11/06 11:23:25 [alert] 20875#0: *60680 open socket #41 left in connection 10
    2015/11/06 11:23:25 [alert] 20875#0: *60678 open socket #46 left in connection 25
    2015/11/06 11:23:25 [alert] 20875#0: *60677 open socket #45 left in connection 26
    2015/11/06 11:23:25 [alert] 20875#0: aborting
    2015/11/06 11:25:04 [notice] 21184#0: signal process started
    2015/11/06 11:25:04 [alert] 21052#0: *97 open socket #3 left in connection 5
    2015/11/06 11:25:04 [alert] 21052#0: *98 open socket #36 left in connection 11
    2015/11/06 11:25:04 [alert] 21052#0: aborting
    2015/11/06 11:27:02 [notice] 21294#0: signal process started
    2015/11/06 11:28:39 [notice] 21378#0: signal process started
    
    • Lety
      Lety over 8 years
    • Mark Topper
      Mark Topper over 8 years
      @ThomasW., I just cleared the log and restarted nginx. And the only thing that comes into it is the line ending with signal process started, and it comes from restarting nginx. Nothing is logged for the Bad Gateway.
    • Mark Topper
      Mark Topper over 8 years
      Thanks @Lety, but I am quite sure this error was from when I had location /* { ... } while back when I tried anything in order to get it to work. It is also a report from long time ago, and nginx have been restarted many times since that.
    • Mark Topper
      Mark Topper over 8 years
      Solved changing proxy_pass to http://... instead of https://.... Feel stupid now... Thanks anyways guys.
    • Thomas Ward
      Thomas Ward over 8 years
      @Mark post that as an answer to your question so you can accept it and mark the question as answered later.