title: Keycloak with Wildfly application behind a Nginx reverse proxy
body: <p>AWS EC2 Ubuntu 18.04
  Keycloak 5.0.0
  Nginx 1.15.8</p>
  
  <p>I am trying to place an application (not my development) based on the Wildfly with the Keycloak integration (openid-connect) and am aware of posts that relate to my task, but I believe my question has not been covered in those posts.</p>
  
  <p>In my case everything works fine, an application and a keycloak server behind a Nginx reverse proxy. What I can't understand is that according to <a href="https://www.keycloak.org/docs/latest/server_installation/index.html#_setting-up-a-load-balancer-or-proxy" rel="nofollow noreferrer">this Keycloak</a> document it is necessary to make the following changes in the keycloak standalone.xml (in my case):</p>
  
  <pre><code>&lt;http-listener name="default" socket-binding="http" proxy-address-forwarding="true" redirect-socket="proxy-https"/&gt;
  
  &lt;socket-binding name="proxy-https" port="443"/&gt;
  </code></pre>
  
  <p>If I understand it correctly, this setup assumes that an application sends an authentication request to http-listener and it is redirected to the proxy-https. It is not quite clear where the proxy is supposed to sent its proxy_pass.</p>
  
  <p>But anyway, in my case, an application sends the authentication request the following way:</p>
  
  <pre><code>       &lt;realm&gt;MyRealm&lt;/realm&gt;
          &lt;resource&gt;MyRealm&lt;/resource&gt;
          &lt;public-client&gt;true&lt;/public-client&gt;
          &lt;auth-server-url&gt;https://&lt;host name&gt;:8843/auth/&lt;/auth-server-url&gt;
          &lt;ssl-required&gt;external&lt;/ssl-required&gt;
  </code></pre>
  
  <p>I have just changed https port in the Keycloak stanalone.xml to 8943 and assigned port 8843 to the Nginx server with the location <code>/auth/</code>, like in this fragment:</p>
  
  <pre><code>server {
  
      listen 192.168.80.40:8843 ssl http2 default_server;
      server_name &lt;host name&gt;;
  
      location /auth/ {
  
  
             proxy_http_version 1.1;
  
  
             proxy_set_header        Host               $http_host;
             proxy_set_header        X-Forwarded-For    $proxy_add_x_forwarded_for;
             proxy_set_header        X-Real-IP          $remote_addr;
             proxy_set_header        X-Forwarded-Proto  $scheme;
             proxy_set_header        X-Forwarded-Port   8843;
             add_header              Strict-Transport-Security "max-age=15552000";
  
             proxy_set_header X-NginX-Proxy true;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
  
             proxy_pass  https://192.168.80.40:8943/auth/;
             proxy_redirect     off;
  
      }
  
    }
  </code></pre>
  
  <p>It works, but I am not quite sure if it is the right way to place a Keycloak server behind the Nginx reverse proxy, considering the above mentioned Keycloak article. It is basically a question not about something that doesn't work but rather why it works.</p>
  
  <p>If some Keycloak experts can assure me that my setup is workable, I would very appreciate it.</p>
  
  <p>My second question:
  Is it possible to restrict an external access only to the application realm, if users decide to open:</p>
  
  <pre><code>https://&lt;host name&gt;:&lt;port&gt;/auth
  </code></pre>
  
  <p>I would like to block any external access to the master realm login screen.</p>
  
  <p>When I am using <code>/auth/realms/MyRealm/</code> in the Nginx location, it does prevent users from accessing the master realm login screen, but it shows just some ugly login screen for the application realm, which actually works, but looks unprofessional. </p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The only solution for my second question I have found so far:</p>
  
  <pre><code>   location /auth/realms/master/ {
  
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  
     proxy_http_version 1.1;
  
  
     proxy_set_header        Host               $http_host;
     proxy_set_header        X-Forwarded-For    $proxy_add_x_forwarded_for;
     proxy_set_header        X-Real-IP          $remote_addr;
     proxy_set_header        X-Forwarded-Proto  $scheme;
     proxy_set_header        X-Forwarded-Port   8843;
     add_header              Strict-Transport-Security "max-age=15552000";
  
     proxy_set_header X-NginX-Proxy true;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
  
     add_header Pragma "no-cache";
  
     proxy_pass  https://192.168.80.40:8943/auth/realms/master/;
     proxy_redirect     off;
  
     allow X.X.X.X;
     deny all;
  </code></pre>
  
  <p>}</p>
  
  <p>And similar for the root location:</p>
  
  <pre><code>       location / {
  
             *
             *
             *
  
         proxy_pass  https://192.168.80.40:8943$request_uri;
         proxy_redirect     off;
  
         allow X.X.X.X;
         deny all;
  
  }
  </code></pre>
  
  <p>The location <code>/auth/</code> is not changed.</p>
  
  <p><code>https://&lt;host name&gt;:&lt;port&gt;/auth</code> still opens <code>"Welcome to Keycloak"</code> screen but access to the <code>"Administration Console"</code> is forbidden. At least, I have now a normal login screen for the application realm and rogue Internet people are going to nowhere from that Welcome screen.</p>
  
  <p>I still need help with my first question.</p>