Nginx returns “502 Bad GateWay” + requestUrl when HTTP request returns 502 status code












0















I want to implement a function.



Nginx returns "502 Bad GateWay" + requestUrl when HTTP request returns 502 status code



How to configure nginx to achieve this function, thank you.



#/usr/local/nginx/lua/auth/404.lua
ngx.say("502 Bad GateWay ")
local request_method = ngx.var.request_method
ngx.say(request_method)
local request_uri = ngx.var.request_uri
ngx.say(request_uri)
#nginx.conf
proxy_intercept_errors on ;
error_page 502 /502.html;
location =/502.html {
content_by_lua_file "/usr/local/nginx/lua/auth/404.lua";
}









share|improve this question





























    0















    I want to implement a function.



    Nginx returns "502 Bad GateWay" + requestUrl when HTTP request returns 502 status code



    How to configure nginx to achieve this function, thank you.



    #/usr/local/nginx/lua/auth/404.lua
    ngx.say("502 Bad GateWay ")
    local request_method = ngx.var.request_method
    ngx.say(request_method)
    local request_uri = ngx.var.request_uri
    ngx.say(request_uri)
    #nginx.conf
    proxy_intercept_errors on ;
    error_page 502 /502.html;
    location =/502.html {
    content_by_lua_file "/usr/local/nginx/lua/auth/404.lua";
    }









    share|improve this question



























      0












      0








      0


      0






      I want to implement a function.



      Nginx returns "502 Bad GateWay" + requestUrl when HTTP request returns 502 status code



      How to configure nginx to achieve this function, thank you.



      #/usr/local/nginx/lua/auth/404.lua
      ngx.say("502 Bad GateWay ")
      local request_method = ngx.var.request_method
      ngx.say(request_method)
      local request_uri = ngx.var.request_uri
      ngx.say(request_uri)
      #nginx.conf
      proxy_intercept_errors on ;
      error_page 502 /502.html;
      location =/502.html {
      content_by_lua_file "/usr/local/nginx/lua/auth/404.lua";
      }









      share|improve this question
















      I want to implement a function.



      Nginx returns "502 Bad GateWay" + requestUrl when HTTP request returns 502 status code



      How to configure nginx to achieve this function, thank you.



      #/usr/local/nginx/lua/auth/404.lua
      ngx.say("502 Bad GateWay ")
      local request_method = ngx.var.request_method
      ngx.say(request_method)
      local request_uri = ngx.var.request_uri
      ngx.say(request_uri)
      #nginx.conf
      proxy_intercept_errors on ;
      error_page 502 /502.html;
      location =/502.html {
      content_by_lua_file "/usr/local/nginx/lua/auth/404.lua";
      }






      http nginx nginx-location nginx-config






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 26 at 6:07







      xuefeng Yang

















      asked Jan 3 at 0:55









      xuefeng Yangxuefeng Yang

      116




      116
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need the proxy_intercept_errors directive.

          The default value of this directive is off. You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). More details about this directive.



          Here is an example configuration file which I've tested.



          upstream tomcat502 {
          server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
          }

          server {
          listen 10019; # it's up to you
          server_name 10.10.100.133;

          location /intercept502 {
          proxy_intercept_errors on; # the most important directive, make it on;
          proxy_pass http://tomcat502/;
          error_page 502 = @502; # redefine 502 error page
          }

          location @502 {
          return 502 $request_urin; # you could return anything you want.
          }
          }


          After reloading nginx, use curl to test it.



          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
          /intercept502
          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
          HTTP/1.1 502 Bad Gateway
          Server: nginx/1.12.1
          Date: Wed, 09 Jan 2019 13:48:05 GMT
          Content-Type: application/octet-stream
          Content-Length: 14
          Connection: keep-alive


          I have added some explanation up in configuration. Hope it would help.






          share|improve this answer
























          • thank you ! Finally solved the problem with a lua script. Thank you.

            – xuefeng Yang
            Jan 25 at 3:50











          • @xuefengYang You could put up your answer if you’ve solved this issue.

            – Light.G
            Jan 25 at 14:22











          • the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

            – xuefeng Yang
            Jan 26 at 6:02













          • github.com/openresty/openresty

            – xuefeng Yang
            Jan 26 at 6:09














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015065%2fnginx-returns-502-bad-gateway-requesturl-when-http-request-returns-502-statu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need the proxy_intercept_errors directive.

          The default value of this directive is off. You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). More details about this directive.



          Here is an example configuration file which I've tested.



          upstream tomcat502 {
          server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
          }

          server {
          listen 10019; # it's up to you
          server_name 10.10.100.133;

          location /intercept502 {
          proxy_intercept_errors on; # the most important directive, make it on;
          proxy_pass http://tomcat502/;
          error_page 502 = @502; # redefine 502 error page
          }

          location @502 {
          return 502 $request_urin; # you could return anything you want.
          }
          }


          After reloading nginx, use curl to test it.



          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
          /intercept502
          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
          HTTP/1.1 502 Bad Gateway
          Server: nginx/1.12.1
          Date: Wed, 09 Jan 2019 13:48:05 GMT
          Content-Type: application/octet-stream
          Content-Length: 14
          Connection: keep-alive


          I have added some explanation up in configuration. Hope it would help.






          share|improve this answer
























          • thank you ! Finally solved the problem with a lua script. Thank you.

            – xuefeng Yang
            Jan 25 at 3:50











          • @xuefengYang You could put up your answer if you’ve solved this issue.

            – Light.G
            Jan 25 at 14:22











          • the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

            – xuefeng Yang
            Jan 26 at 6:02













          • github.com/openresty/openresty

            – xuefeng Yang
            Jan 26 at 6:09


















          0














          You need the proxy_intercept_errors directive.

          The default value of this directive is off. You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). More details about this directive.



          Here is an example configuration file which I've tested.



          upstream tomcat502 {
          server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
          }

          server {
          listen 10019; # it's up to you
          server_name 10.10.100.133;

          location /intercept502 {
          proxy_intercept_errors on; # the most important directive, make it on;
          proxy_pass http://tomcat502/;
          error_page 502 = @502; # redefine 502 error page
          }

          location @502 {
          return 502 $request_urin; # you could return anything you want.
          }
          }


          After reloading nginx, use curl to test it.



          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
          /intercept502
          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
          HTTP/1.1 502 Bad Gateway
          Server: nginx/1.12.1
          Date: Wed, 09 Jan 2019 13:48:05 GMT
          Content-Type: application/octet-stream
          Content-Length: 14
          Connection: keep-alive


          I have added some explanation up in configuration. Hope it would help.






          share|improve this answer
























          • thank you ! Finally solved the problem with a lua script. Thank you.

            – xuefeng Yang
            Jan 25 at 3:50











          • @xuefengYang You could put up your answer if you’ve solved this issue.

            – Light.G
            Jan 25 at 14:22











          • the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

            – xuefeng Yang
            Jan 26 at 6:02













          • github.com/openresty/openresty

            – xuefeng Yang
            Jan 26 at 6:09
















          0












          0








          0







          You need the proxy_intercept_errors directive.

          The default value of this directive is off. You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). More details about this directive.



          Here is an example configuration file which I've tested.



          upstream tomcat502 {
          server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
          }

          server {
          listen 10019; # it's up to you
          server_name 10.10.100.133;

          location /intercept502 {
          proxy_intercept_errors on; # the most important directive, make it on;
          proxy_pass http://tomcat502/;
          error_page 502 = @502; # redefine 502 error page
          }

          location @502 {
          return 502 $request_urin; # you could return anything you want.
          }
          }


          After reloading nginx, use curl to test it.



          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
          /intercept502
          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
          HTTP/1.1 502 Bad Gateway
          Server: nginx/1.12.1
          Date: Wed, 09 Jan 2019 13:48:05 GMT
          Content-Type: application/octet-stream
          Content-Length: 14
          Connection: keep-alive


          I have added some explanation up in configuration. Hope it would help.






          share|improve this answer













          You need the proxy_intercept_errors directive.

          The default value of this directive is off. You must turn it on if you want to intercept response from proxied server with status code bigger/equal than 300(of course, 502 included). More details about this directive.



          Here is an example configuration file which I've tested.



          upstream tomcat502 {
          server 10.10.100.131:28889; # There is no such a backend server, so it would return 502
          }

          server {
          listen 10019; # it's up to you
          server_name 10.10.100.133;

          location /intercept502 {
          proxy_intercept_errors on; # the most important directive, make it on;
          proxy_pass http://tomcat502/;
          error_page 502 = @502; # redefine 502 error page
          }

          location @502 {
          return 502 $request_urin; # you could return anything you want.
          }
          }


          After reloading nginx, use curl to test it.



          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502
          /intercept502
          [root@test133 lunatic]# curl http://10.10.100.133:10019/intercept502 -I
          HTTP/1.1 502 Bad Gateway
          Server: nginx/1.12.1
          Date: Wed, 09 Jan 2019 13:48:05 GMT
          Content-Type: application/octet-stream
          Content-Length: 14
          Connection: keep-alive


          I have added some explanation up in configuration. Hope it would help.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 13:48









          Light.GLight.G

          475313




          475313













          • thank you ! Finally solved the problem with a lua script. Thank you.

            – xuefeng Yang
            Jan 25 at 3:50











          • @xuefengYang You could put up your answer if you’ve solved this issue.

            – Light.G
            Jan 25 at 14:22











          • the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

            – xuefeng Yang
            Jan 26 at 6:02













          • github.com/openresty/openresty

            – xuefeng Yang
            Jan 26 at 6:09





















          • thank you ! Finally solved the problem with a lua script. Thank you.

            – xuefeng Yang
            Jan 25 at 3:50











          • @xuefengYang You could put up your answer if you’ve solved this issue.

            – Light.G
            Jan 25 at 14:22











          • the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

            – xuefeng Yang
            Jan 26 at 6:02













          • github.com/openresty/openresty

            – xuefeng Yang
            Jan 26 at 6:09



















          thank you ! Finally solved the problem with a lua script. Thank you.

          – xuefeng Yang
          Jan 25 at 3:50





          thank you ! Finally solved the problem with a lua script. Thank you.

          – xuefeng Yang
          Jan 25 at 3:50













          @xuefengYang You could put up your answer if you’ve solved this issue.

          – Light.G
          Jan 25 at 14:22





          @xuefengYang You could put up your answer if you’ve solved this issue.

          – Light.G
          Jan 25 at 14:22













          the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

          – xuefeng Yang
          Jan 26 at 6:02







          the code as this: ngx.say("502 Bad GateWay ") local request_method = ngx.var.request_method ngx.say(request_method) local request_uri = ngx.var.request_uri ngx.say(request_uri) proxy_intercept_errors on ; error_page 502 /502.html; location =/502.html { content_by_lua_file "/usr/local/nginx/lua/auth/404.lua"; }

          – xuefeng Yang
          Jan 26 at 6:02















          github.com/openresty/openresty

          – xuefeng Yang
          Jan 26 at 6:09







          github.com/openresty/openresty

          – xuefeng Yang
          Jan 26 at 6:09






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015065%2fnginx-returns-502-bad-gateway-requesturl-when-http-request-returns-502-statu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$