HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Updated nginx docs

LincLinc Admin
edited December 2018 in Tutorials

So you know, I made extensive updates to Vanilla's nginx setup docs tonight to address a bunch of problems I ran into when setting up a fresh server install. I hope it clarifies a number of points that were difficult to follow before.

«1

Comments

  • Reverting to a deprecated server configuration rather than debugging the issue is a temporary and incomplete solution. We've already started dropping support for the 'p' parameter and it's only going to accelerate in the future.

  • RiverRiver MVP
    edited February 2019

    P3ter said:

    Hello,

    I tried to install Vanilla 2.6.4 on a Nginx webserver, using this documentation, but I have a "to many redirect" error on http://localhost/dashboard/setup

    Nginx conf : https://framabin.org/p/?cf4599d91224ddee#bWE/wLo9jrFL1g+m7dYBqpZIJP13GQRVMo7o2pYWoO0=

    But, I finaly get it work using this nginx conf (found it on this forum) : https://framabin.org/p/?b5bbeb1018e47783#qmq3PSU9V2fcXbFOVK0GQjDHwvZjnViqH8H3z8r/tqY=

    Probably something in the rewrite rules is wrong on the first nginx conf (with the second nginx conf, vanilla url is : http://localhost/index.php?p=/dashboard/setup).

    --------



    thanks P3ter for reporting your solution and workaround, if someone else runs into issue.

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • charrondevcharrondev Vanilla Staff

    @P3ter @River

    It may also help to take a look at our vanilla-docker nginx config. This is what we use for our localhosts, and has some similarities to what we run in production. https://github.com/vanilla/vanilla-docker/blob/master/resources/etc/nginx/sites-available/dev.vanilla.localhost.conf

    I may attempt a fresh installation soon though, and I'll update the documented configuration afterwards.

  • nervoustwitnervoustwit Vanilla Staff

    Helpful.

  • Thank you charrandev ! Your Nginx conf file works great for me (after changing fastcgi_pass to use "unix:/run/php/php7.2-fpm.sock" instead of 'php-fpm").

  • edited February 2019

    Thanks for the update, Linc! The provided config works good, but my PATH_INFO was returning empty. I also have PHP's cgi.fix_pathinfo set to its default setting (enabled). I fixed it by adding fastcgi_split_path_info ^(.+?\.php)(/.*)$; in my location ~* ^/index\.php(/|$) { ... } block to get PATH_INFO to report the correct path.

    It works well now and I appreciate the extra hardening measures you put in.

    On the other hand, if Vanilla is in a subdirectory, it seems that the nginx config will need some adjustments, does anyone have any ideas?

    Add Pages to Vanilla with the Basic Pages app

  • I'm having no luck getting rewrite to work with a new install, on Ubuntu 18.04 nginx.

    The site works fine without

    fastcgi_param X_REWRITE 1;

    showing the pages as expected, with the index.php?p addition.

    Once REWRITE is set to 1 in the location block, the correct URLs of the pages show without the index.php?p, however the pages don't actually load.

    I'm guessing the issue is that my PATH INFO is showing empty. I added the line suggested by @Shadowdare but nothing changed. I set cgi.fix_pathinfo=1 in php.ini, but there's obviously something else I haven't done.

    If anyone knew what that was, and could tell me, I'd be very grateful.

  • edited February 2019

    @whu606, setting the fastcgi_param X_REWRITE is the new way to enable RewriteUrls that was previously done in the config, so that's good. By pages not loading, are they just blank, and at any URL (not just index.php, but perhaps /activity for example)? I'm going to guess that the file isn't getting passed to php-fpm. If any, the error might have gotten logged in nginx's error log that you can check.

    What else does the var_export show when you do the troubleshooting steps, besides a blank PATH_INFO?

    Add Pages to Vanilla with the Basic Pages app

  • @Shadowdare

    Thanks for the reply.

    As an example, if I am on the main index page

    and click on a recent discussion link

    the link changes as expected, but the page remains on index.php.

    Without REWRITE on, the forum works exactly as expected, so it isn't an nginx error as such, as far as I can tell. I can only assume there is some parameter that I have not set correctly.

  • Here is part of the export_var file (couldn't paste it as text or code...)



  • edited March 2019

    @whu606, I see what you mean now. Your var_export looks okay except for PATH_INFO. I had that issue before when I didn't have fastcgi_split_path_info ^(.+?\.php)(/.*)$;.

    My main nginx rules (other parts omitted) look like this:

        location ~* ^/index\.php(/|$) {
            include fastcgi_params;
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_param SCRIPT_NAME /index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
            fastcgi_param X_REWRITE 1;
            fastcgi_pass php-fpm;
        }
    
        # If this is some other PHP script, disallow it by redirecting to /index.php
        location ~* \.php(/|$) {
            rewrite ^ /index.php$uri last;
        }
    
        # Default path handling
        location / {
            try_files $uri @vanilla;
        }
        
        location @vanilla {
            rewrite ^ /index.php$uri last;
        }
    

    Make sure your fastcgi_params or fastcgi.conf file has the lines from this link, too, and restart nginx accordingly: https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/#fastcgi-params

    Also, you should add a file with <?php phpinfo(); ?> to check that cgi.fix_pathinfo is 1.

    Add Pages to Vanilla with the Basic Pages app

  • Thnaks.

    pathinfo is definitely 1.

    I'll have a further go later.

  • @Shadowdare

    Thanks for your help.

    After fiddling about, and a lot of copy and pasting and restarting nginx, I now have rewrites almost working.

    All pages with a //serveraddress/slug work as expected.

    However, just using //serveraddress (which, afaIk should point automatically to index.php) throws a 404 error.

    //serveraddress/index.php loads just as expected.

    As far as I can see my nginx conf file now looks pretty much as yours or the Vanilla example does.

    location ~* ^/index\.php(/|$) {
     include fastcgi_params;
     fastcgi_split_path_info ^(.+?\.php)(/.*)$;
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
       fastcgi_param X_REWRITE 1;
       include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_NAME /index.php;
           fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
     }
      location ~* \.php(/|$) {
           rewrite ^ /index.php$uri last;
       }
        location / {
           try_files $uri @vanilla;
       }
     location @vanilla {
           rewrite ^ /index.php$uri last;
       }
     location ~ /\.ht {
       deny all;
     }
    

    I've fiddled with

    fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
    

    but tbh, I can change that, or even delete that line, and the site still works, and the issue persists.

    Any suggestions?

  • charrondevcharrondev Vanilla Staff

    @whu606 Have you tried adding the following at the top level of the server block?

    index index.php;
    
  • @charrondev

    Thanks for responding. At the start of the server block I have:

     index index.php;
    

    just below the 'root' line (couldn't post that bit, as Cloudflare didn't like it...)

    Everything works fine provided there is a slug following the server IP - /discussion, /activity, /notifications, /dashboard etc. It is only if I type the server IP alone (or click Vanilla or Home) that I get a 404 error.

  • edited March 2019

    @whu606, I also have the 'index' line below the 'root' line. Mine is index index.php index.html index.htm;.

    Is there anything special in snippets/fastcgi-php.conf?

    At '//serveraddress', is the 404 error a generic nginx 404 error page or Vanilla's "page not found" view?

    Also, not sure if the order matters, but putting the fastcgi_pass line last after all the param lines might do something.

    Add Pages to Vanilla with the Basic Pages app

  • whu606whu606 MVP
    edited March 2019

    @Shadowdare

    I've got it working as expected, after more trial and error cutting bits out of files, pasting bits into files, moving lines around, and seeing what happens...

    Couldn't swear to what I changed, if I'm honest, but at least it is working.

    Just for reference, it was the generic nginx 404 error.

    Thanks for your support.

  • can someone update https://docs.vanillaforums.com/developer/backend/server-nginx/

    The suggested nginx conf did not work with v2.8 as well as those found in github/vanilla.

    Only the above got rewrite working and Api v2 working.

Sign In or Register to comment.