WordPress.org

Ready to get started?Download WordPress

Forums

nginx - Network admin pointing to root as opposed to subdirectory (85 posts)

  1. hgrianevans
    Member
    Posted 1 year ago #

    Mika, I'm wondering...could WP be getting some variable inccorect through the fastcgi?

    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT $document_root;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;

  2. FastCGI shouldn't matter. Nothing in that tells WP where it is.

    Only three things tell WP it's address:

    1) Where it's actually installed ;)
    2) You (manually editing things, which we know you didn't do)
    3) .htaccess/nginx

    Well... maybe 4 would be 'httpd.conf settings' btu that would have shown up earlier.

  3. hgrianevans
    Member
    Posted 1 year ago #

    Hmmm...this has got me thinking...

    Since my non-wp site is served under http://www.domain and I created a non-www server section for nginx, I might as well go whole hog and create a specific document root for wp, thus allowing me to install WP in that root.

    e.g.

    server {
    listen 80;
    root /usr/local/nginx/htdocs;
    server_name www.domain.com;
    ...
    }
    server {
    listen 80;
    root /usr/local/nginx/WP-docs;
    server_name domain.com;
    ...
    }

    This might work...will let ya know.

  4. Pothi
    Member
    Posted 1 year ago #

    Looking at some of the nginx config examples, it seems it would be much easier if all of this was in the root, but as I've pointed out, my main non-wp site's already there.

    @hgrianevans You have an interesting issue to solve (but, not hard to solve).

    location ^~ /blogs

    The conf is telling WP it's really in the main folder. (The .htaccess for Multisite is different than single install, so I believe they're different for nginx too)

    I was reading http://wiki.nginx.org/WordPress and it has a different explanation of how to do subfolders, and it has the multisite info, but I know every server's a little different.

    Very true. From what I see from Mika's answers, there is nothing wrong with your particular WordPress installation as a multisite on a sub-directory. The nginx configuration for a single site and multisite are quite different. If you (@hgrianevans) followed the nginx configuration for a multisite from http://wiki.nginx.org/WordPress or from http://wiki.nginx.org/WordPress , everything should work as expected (or at least, you can make them work, if you tweak a little for your specific situation; multisite on a sub-directory).

    Since my non-wp site is served under http://www.domain and I created a non-www server section for nginx, I might as well go whole hog and create a specific document root for wp, thus allowing me to install WP in that root.

    A good idea indeed. But, if your nginx configuration is flawed, then you'd still have some issues accessing the dashboard for the sites created under multisite.

  5. hgrianevans
    Member
    Posted 1 year ago #

    Hi Pothi,

    What's confusing me...I understand how nginx can rewrite an URL to handle it internally, but I'm not understanding how nginx could make WP display an incorrect URL in WP's menus.

  6. Pothi
    Member
    Posted 1 year ago #

    Hello hgrianevans,

    I'm not understanding how nginx could make WP display an incorrect URL in WP's menus.

    It's strange indeed.

    BTW, I just tested what you were trying to achieve. Here is what I got without any issues...

    elegantblogs . net (a standalone installation based on the latest version of WP)
    above URL + /blogs/ (a multisite installation based on trunk version; you can see 2012 theme)
    /blogs/blog1/ (a site under multisite)
    /blogs/blog2/ (another site under multisite)

    Update: The above URL may go off after a few days (as it is only my test domain). It runs on Nginx, but my "Server" header is overwritten by CloudFlare. I have my own nginx configuration for multisite. You can check it out at https://github.com/pothi/WordPress-Nginx/blob/master/sites-enabled/mu-dir-domainname.com.conf (please remove the two spaces around the dot to get the URL).

  7. hgrianevans
    Member
    Posted 1 year ago #

    do the network admin urls in wp-admin point to the correct URL? That's the issue I was seeing.

  8. Pothi
    Member
    Posted 1 year ago #

    Yes, they do.

  9. hgrianevans
    Member
    Posted 1 year ago #

    I'll give it a try in a few hours.

  10. hgrianevans
    Member
    Posted 1 year ago #

    Pothi,

    Quick question: how would you change your config to redirect any urls not /blogs or below back to the http://www.domain.com server block?

    Thanks.

  11. rahul286
    Member
    Posted 1 year ago #

    @hgrianevans

    You can try - http://rtcamp.com/tutorials/wordpress-nginx-multisite-subdirectories-in-subdirectory-itself/

    It is already tested and working config.

    You just need to make 2 changes in it - domain name & folder name.

  12. rahul286
    Member
    Posted 1 year ago #

    Quick question: how would you change your config to redirect any urls not /blogs or below back to the http://www.domain.com server block?

    In my config - if a file exist in root dir, it will be used first. So I guess you will not face that issue!

  13. hgrianevans
    Member
    Posted 1 year ago #

    @rahul286

    I'll take a look at your config too.

    Just to be clear on my question...my non-wordpress php driven site is currently running on a http://www.domain.com nginx server block.

    Because WP prefers we drop the 'www' I'm testing with a non-www server block for just the WP URLs. Just in case someby takes a non-WP url from my site:

    http://www.domain.com/somedir/somefile and tries dropping the 'www' I want to make sure that the wordpress server block redirects non-WP requests back to http://www.domain.com. Is there an efficient way for nginx to do this?

  14. hgrianevans
    Member
    Posted 1 year ago #

    @rahul286

    ...and we're back to square one. Followed your exact directions in http://rtcamp.com/tutorials/wordpress-nginx-multisite-subdirectories-in-subdirectory-itself/

    Did a fresh install of WP database and folder. Installed WP, installed network and sadly the Network Admin URLs on the dashboard still point to wp-admin in root even though it's in /blogs.

    I'm officially in WordPress/Nginx hell. Will try Pothi's setup later but right now I need to get as far away from my keyboard as possible. :-)

  15. Pothi
    Member
    Posted 1 year ago #

    Pothi,

    Quick question: how would you change your config to redirect any urls not /blogs or below back to the http://www.domain.com server block?

    Thanks.

    By adding a location / { try_files $uri /index.php; } block as you can see in my updated Github configuration entry at https://github.com/pothi/WordPress-Nginx/blob/master/sites-enabled/mu-dir-dir-domainname.com.conf .

    Update:

    Basically, you would have at least two location blocks...

    - One for WordPress that looks like location /blogs { }
    - One for the non-WordPress site at the root, like location / { }

    I hope this helps.

  16. hgrianevans
    Member
    Posted 1 year ago #

    Pothi,

    Going to try your config in a few minutes after a very late dinner.

    Looking at the location / though the use case should be:

    http://www.domainname.com has a non-WordPress installation at its root (note the www) so I need to redirect to www anything that's not php.

    Thanks!

  17. Pothi
    Member
    Posted 1 year ago #

    You are right.

    In my configuration in Github, there is a bug (that I fixed now). The root should point to the path of the non-WordPress installation.

  18. rahul286
    Member
    Posted 1 year ago #

    @hgrianevans

    You need to check root directive and also all try_files directive.
    If you only change root path, try_files may fail.

    To make things simple... U can paste your conf on https://gist.github.com/ so that we can have a look at it.

    To crosscheck my config, I created a demo setup at http://sub.rtcamp.net/blogs/
    I also put a physical file in root dir and its working fine - http://sub.rtcamp.net/info.php

    Though if you want to host another wordpress or any application which relies on rewrite-rules in http://sub.rtcamp.net/ then things will be slightly different.

  19. hgrianevans
    Member
    Posted 1 year ago #

    @rahul286,

    The conf I used earlier this evening was yours copied and pasted from http://rtcamp.com/tutorials/wordpress-nginx-multisite-subdirectories-in-subdirectory-itself/

    I'm just going to test Pothi's config, but since wordpress is only dealing with the server block built specifically for it, I'm at a loss (as as Mika, I think) why WP keeps creating URLs that point to the root.

  20. hgrianevans
    Member
    Posted 1 year ago #

    Pothi,

    Thanks for letting me see your config (which I'll test in a moment) but I think you're misunderstanding my question.

    Your config currently has:

    # Redirect www.domainname.com/anything to domainname.com/anything
    server {
    listen 80;
    server_name www.domainname.com;
    return 301 $scheme://domainname.com$request_uri;

    I'm talking about redirecting domainname.com/anythingthatisnotwordpress to www.domainname.com/anything as my site (and all it's Google love, etc) is at WWW. and I'm only dropping the "www" for Worpdress per Mika's suggestion.

  21. rahul286
    Member
    Posted 1 year ago #

    but since wordpress is only dealing with the server block built specifically for it,

    Actually WordPress is not dealing with anything itself.
    Its nginx decision to pass some requests to different part of wordpress for certain conditions.

    2 questions:

    • Do you have any wordpress-like app in root-dir
    • This may sound lame, but have you made changes to my config - there are many things you need to change: path, domain, folder-name, fastcgi_pass value, etc

    Also, wordpress wp-config will have following in your case: (if there is a mistake there)

    define('MULTISITE', true);
    define('SUBDOMAIN_INSTALL', false);
    $base = '/blogs/';
    define('DOMAIN_CURRENT_SITE', 'yourdomain.com');
    define('PATH_CURRENT_SITE', '/blogs/');
    define('SITE_ID_CURRENT_SITE', 1);
    define('BLOG_ID_CURRENT_SITE', 1);

    Can you verify value for $base and others?

  22. hgrianevans
    Member
    Posted 1 year ago #

    I created a specific document root for the WP (non-www) server block and created a /blogs subir in that.

    Yes, I changed the path, domain, folder-name, fastcgi_pass value, etc to my values. The install at domain.com/blogs worked fine EXCEPT the URLs for network admin point to root instead of /blogs and yes, the wp-config changes WP suggested are wrong too as the install suggested:

    define('MULTISITE', true);
    define('SUBDOMAIN_INSTALL', false);
    $base = '/'; <-- WRONG
    define('DOMAIN_CURRENT_SITE', 'domain.com');
    define('PATH_CURRENT_SITE', '/'); <-- WRONG
    define('SITE_ID_CURRENT_SITE', 1);
    define('BLOG_ID_CURRENT_SITE', 1);

    As Mika and I have mentioned above we're not sure why a /blogs install that appears to work makes urls and config suggestions pointing to root. It's driving me nuts. :-)

  23. Pothi
    Member
    Posted 1 year ago #

    I'm talking about redirecting domainname.com/anythingthatisnotwordpress to http://www.domainname.com/anything as my site (and all it's Google love, etc) is at WWW. and I'm only dropping the "www" for Worpdress per Mika's suggestion.

    @hgrianevans
    You are right. I didn't understand what you meant earlier and now. :)

    Anyway, let me clarify my configuration...

    The following server block...

    # Redirect www.domainname.com/anything to domainname.com/anything
    server {
    listen 80;
    server_name www.domainname.com;
    return 301 $scheme://domainname.com$request_uri;
    }

    doesn't really care, if you use WordPress or non-WordPress based site or if you use both. :) It takes care that your domain is served consistently with (or without) 'www', depending on what you choose to do with your domain name.

    If your site uses 'www', you don't have to drop it to use your non-WP site at the root and your WordPress site at 'blogs' sub-directory. Here's what it'd look like, if my/your site is with 'www'...

    # Redirect domainname.com/anything to www.domainname.com/anything
    server {
    listen 80;
    server_name domainname.com;
    return 301 $scheme://www.domainname.com$request_uri;
    }
    
    # Process requests to www.domainname.com
    server {
    listen 80;
    server_name www.domainname.com;
    index index.php;
    ....
  24. rahul286
    Member
    Posted 1 year ago #

    Also, www issue can be fixed separately!

    My advise would be to use common server{..} block in your case (as your main site needs www prefix)

    Add following as a first rule...

    server {
         server_name example.com www.example.com;
         #other stuff
         if ($host = 'www.example.com' ) {
              rewrite ^/wordpress/(.*)$ http://example.com/wordpress$1 permanent;
         }
         #other stuff
    }

    Above has a cavet that part of sites outside /wordpress/ folder will be accessible using WWW as well as non-WWW.
    You can and another condition with some more work. Nginx doesn't support NEGATION in regex/location rules so be careful on this part.

  25. hgrianevans
    Member
    Posted 1 year ago #

    @rahul286,

    I'm so coffee-deprived right now! [gotta make a mug!]

    Sadly the fact that non-WP can be accessed with or without www is a problem because there are tens of thousands of pages in the non-WP parts of the site and I'd be afraid of Google duplicate issues.

    The part that's really, really not understandable is why as I showed above that 90% of the URLs are pointing correctly to /blogs and the network admin ones are pointing to /wp-admin and not /blogs/wp-admin. What the heck is happening in the install process that causes that? Is it grabbing some malformed PHP variable during the install process?

    I thank all you guys. I just wish I could understand the install errors.

  26. Pothi
    Member
    Posted 1 year ago #

    My advise would be to use common server{..} block in your case (as your main site needs www prefix)

    IMO, it only complicates things further (we already see a complication in the way the WordPress is recommended to be installed without 'www'). Instead, it can be done with two server blocks. Please let me clarify...

    server {
      server example.com;
    
      # usual stuff to process WP multisite install
    
      location /wpmuinstall {
         # try_files
      }
    
      location / {
          return 301 $scheme://www.example.com$request_uri;
      }
    }
    
    server {
      server_name www.example.com;
    
      # usual stuff to process non-WP site
    
      location /wpmuinstall {
        return 301 $scheme://example.com$request_uri;
      }
    
      location / {
        # try_files
      }
    
    }
  27. hgrianevans
    Member
    Posted 1 year ago #

    Pothi,

    Kettle's on...brain's getting ready. :-)

    Just curious, why do you need a try_files in the non-WP section? Isn't the try files stuff just for WP? Or is it beneficial for the non-WP stuff as well?

  28. rahul286
    Member
    Posted 1 year ago #

    @hgrianevans

    try_files is not wordpress specific

    @Pothi

    hgrianevans wants to use mix on www and non-www. I generally use separate blocks but his case common block will make it easy to maintain config going ahead.

    About your config, its clean and nice but in practice there will be many more location blocks (php file, static assest, etc) which will need to be copied in both places.
    location cannot be used outside server{..} block otherwise I would have voted for your config.

    Also, 2-server blocks will become further complicate if user decide to use plugins like super-cache and root-site also uses wordpress! Just imagine where it will lead...

  29. rahul286
    Member
    Posted 1 year ago #

    @hgrianevans

    I did not say main site will be left as it is. Just some more rules needed to force WWW for main site.

    I cannot provide them as I don't know your about your main-site :|

    Its hard to guess exact rules you need without seeing your main site

  30. Pothi
    Member
    Posted 1 year ago #

    Just curious, why do you need a try_files in the non-WP section? Isn't the try files stuff just for WP? Or is it beneficial for the non-WP stuff as well?

    As rahul286 has already mentioned, try_files is not WP specific. Usage of try_files is entirely optional, depending on your Nginx configuration of your non-WP site. Just replace it with your own configuration for the non-WP site.

Topic Closed

This topic has been closed to new replies.

About this Topic