[Solved] (Grav-Admin) Nginx Load Balancer w/ Backend Nginx Setup: Broken User Creation

Hi everyone, hoping I could get some assistance here.

My setup is as follows:

Box1 Frontend (Load Balancer): Nginx
Box2 Backend: Nginx, PHP, Grav (with admin)

Myurl points to box 1 which does a proxy to box 2.

The initial page (/admin) to create a user loads fine when I hit myurl, however after I click on create user all I get is oops something went wrong.

Nothing valuable the nginx logs on both box1/box2.

Now, if I connect directly to the IP of box2 (backend), user creation completes successfully (and of course I get the token issue when accessing again via myurl, but that part is expected).

Frontend (note astericked out actual site)
nginx.conf is pretty much standard on frontend

sites-enabled# cat hosted-aperturious

upstream aperturious {
        server 192.168.223.69:8086;
}

server {
        listen 80;
        listen [::]:80;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name *******;


        location / {
                proxy_pass http://aperturious;
                proxy_set_header Proxy "";
        }


        listen 443 ssl; # managed by Certbot

        ssl_certificate /etc/letsencrypt/live/*******/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/*******/privkey.pem; # managed by Certbot

        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot


        if ($scheme != "https") {
                return 301 https://$host$request_uri;
        } # managed by Certbot


}

Backend

sites-available# cat hosted-aperturious

        server {
        listen 8086 default_server;
        listen [::]:8086 default_server;

        root /var/www/hosted/aperturious;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                try_files $uri $uri/ /index.php?_url=$uri&$query_string;
        }


        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_param HTTP_PROXY "";

                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }


        # Security Enhancements
        location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
        location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
        location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
        location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
        # End Security Enhancements

        location ~ /\.ht {
                deny all;
        }




}

backend nginx.conf
nginx# cat nginx.conf

user www-data;
worker_processes auto;
worker_rlimit_nofile 8192;
pid /run/nginx.pid;

events {
        use epoll;
        worker_connections 8000;
        multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        client_max_body_size 20m;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 5;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_min_length 256;
        gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;

        # disable content type sniffing for more security
        add_header "X-Content-Type-Options" "nosniff";

        # force the latest IE version
        add_header "X-UA-Compatible" "IE=Edge";

        # enable anti-cross-site scripting filter built into IE 8+
        add_header "X-XSS-Protection" "1; mode=block";
        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Adding some additional information. I’ve added error_log statements within Utils.php.

It does appear to be something with session tokens/nonce. Looks to be for whatever reason we are regenerating after clicking create user. Isset on the static nonce second time through shows as it isn’t being set so it regenerates…

As a note, this is a fresh install and this is the admin create a user page that has loaded.

Landing on page*

2017/09/18 15:44:09 [error] 8621#8621: *19 FastCGI sent in stderr: "PHP message:

PHP message: getNonce(),isset
PHP message:

PHP message: generateNonceString():form
PHP message: generateNonceString():34856|form||h61hv25rm7a57vgtkg6ftlcb22|bj8mytEdlifRYN
PHP message:

PHP message: getNonce(),action: form
PHP message: getNonce(),nonce: 883b94db1c87ac8fbdde274a1b5d4dbe
PHP message: getNonce(),static nonce action: 883b94db1c87ac8fbdde274a1b5d4dbe" while reading response header from upstream, client: ~~~~, server: _, request: "GET /admin HTTP/1.0", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "~~~"

Clicking Create User

2017/09/18 15:44:26 [error] 8621#8621: *21 FastCGI sent in stderr: "PHP message:

PHP message: verifyNonce() action:form
PHP message: verifyNonce() E1:883b94db1c87ac8fbdde274a1b5d4dbe
PHP message:

PHP message: getNonce(),isset
PHP message:

PHP message: generateNonceString():form
PHP message: generateNonceString():34857|form||cc3ta9bun30pj03nul92k8b423|bj8mytEdlifRYN
PHP message:

PHP message: getNonce(),action: form
PHP message: getNonce(),nonce: 6c90889b785774cc9909332d60d3ced1
PHP message: getNonce(),static nonce action: 6c90889b785774cc9909332d60d3ced1
PHP message: verifyNonce() E2:6c90889b785774cc9909332d60d3ced1
PHP message:

PHP message: getNonce(),isset1
PHP message:

PHP message: getNonce(),isset1
PHP message:

PHP message: getNonce(),isset1" while reading response header from upstream, client: ~~~, server: _, request: "POST /admin HTTP/1.0", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "~~~", referrer: "https://~~~.com/admin"

You may have to set some forwarding and host forwarding headers.

Box 2 should know its domain as well-

Thanks for your reply.

I believe i tried with the LB having the following directive:

proxy_set_header Host            $host;
proxy_set_header X-Forwarded-For $remote_addr;

I’ll try to reconfirm tonight or tomorrow. As a note, the logs you grabbed the ~.com from are from Box 2.

I would be surprised though that this would be the cause of the $nonce not persisting (as seen by isset).

I’ll post back with updates after reconfirming on host/x-forwarded for

Well… sweet. I must have not tried the above. Thanks!!

Maybe a combo of items, so I’ll post some relevant items that I can remember…

fpm/php.ini

session.use_strict_mode = 0
session.use_cookies = 1

; http://php.net/session.cookie-secure
;session.cookie_secure =

session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0

in server block of backend:

set_real_ip_from 192.168.212.77; <-- note this is your lb

commented out ipv6 references

on load balancer site enabled

location / {
        proxy_pass http://aperturious;
        proxy_set_header Proxy "";
        proxy_set_header Host            $host;
        proxy_set_header X-Forwarded-For $remote_addr;
}