1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Setup SEO Full Friendly URLs on nginx

Discussion in 'Tips and Guides [Archive]' started by mlx, Oct 6, 2010.

  1. mlx

    mlx Well-Known Member

    It's actually really really simple.

    Considering you have uploaded XenForo into the directory "community", just add this to your nginx config:
    Code:
            location /community/ {
                index  index.php index.html index.htm;
                try_files  $uri $uri/ /community/index.php?$uri&$args;
            }
    
    While you are at it you might also want to add this to block external access to the folders "internal_data" and "library".
    Code:
            location ~ ^/community/(internal_data|library)/(.*)$ {
                internal;
            }
    
    Restart nginx and enable Full Friendly URLs.
     
    Blandt, Pyro, SneakyDave and 3 others like this.
  2. dotted

    dotted Member

    Using 'if' is a bad idea, you should look into try_files

    Code:
    location / {
        try_files $uri $uri/ /index.php;
    }
     
    Jesepi, mlx, Blandt and 1 other person like this.
  3. Jesepi

    Jesepi Well-Known Member

    Could you explain why it is bad? I'd like to know!
     
  4. Sir nick

    Sir nick Well-Known Member

    Please explain as i use if a lot
     
  5. mlx

    mlx Well-Known Member

    Oh interesting IfIsEvil

    I have to admit I didn't know that. Thanks for the heads up dotted.
     
  6. Jesepi

    Jesepi Well-Known Member

    Wow, nice to know.

    Question though. Right now here is how I do my URLs with drupal in nginx (drupal is installed in the "root" of my site):

    Code:
    location / {
    	root  /home/username/www;
            index index.html index.htm index.php;
            try_files $uri $uri/ @drupal;
    
    }
    
    location @drupal {
    rewrite ^/(.*)$  /index.php?q=$1 last;
    }
    
    
    Which works fine, but if I try to add this:


    Code:
    location ~ /xf/ {
        try_files $uri $uri/ /index.php;
    }
    
    


    I get an inaccessible XenForo. No errors or anything in the server logs. It just gives me a "blank" drupal page. I'm guessing there is a conflict I'm not seeing?
     
  7. Blandt

    Blandt Well-Known Member

    The If block forces nginx to test the statement over and over even if the statement is true, it will keep testing which makes the response a lot slower.
    Try_files it will stop the test and proceed as soon as the statement is true ---> a peppy response :D
     
  8. Blandt

    Blandt Well-Known Member

    Code:
    location / {
    	root  /home/username/www;
            index index.html index.htm index.php;
            try_files $uri $uri/ @drupal;
    
    }
    
    location @drupal {
    rewrite ^/(.*)$  /index.php?q=$1 last;
    }
    
    
    Which works fine, but if I try to add this:


    Code:
    [COLOR=rgb(255, 0, 0)]location ~ /xf/ {[/COLOR]
        try_files $uri $uri/ /index.php;
    }
    
    

    [/quote]

    Is that your xf installation ? you need to put it in a location outside of the / location
     
  9. Jesepi

    Jesepi Well-Known Member

    yes, the 'xf' directory is inside my root folder. I thought location rules in nginx went for the most specific available rule? So something like

    location / { }
    location /pets { }
    location /pets/cat { }

    if you were accessing /pets/cat the previous 2 would be disregarded?
     
  10. Sir nick

    Sir nick Well-Known Member

    K thanks, cause my nginx box uses if's a lot.
     
  11. Blandt

    Blandt Well-Known Member

    Yes it will go to the most specific folder.

    But your root document in this case doesn't reside in xf folder

    Let me see your whole config. PM if you want privacy
     
  12. Jesepi

    Jesepi Well-Known Member

    I'd rather keep it public in case anyone else has the problem and searches for it, so I just changed the names used.

    Code:
    ## move the www people to no-www
    server {
        listen   80;
        server_name  www.mydomain.net;
        rewrite ^/(.*) http://mydomain.net/$1 permanent;
    
    }
    ###########
    server {
        listen  80;
        server_name mydomain.net;
        access_log  /var/log/nginx/mydomain.access.log;
    
    
    location / {
    	root   /home/mydomain/www;
            index index.html index.htm index.php;
            try_files $uri $uri/ @drupal;
    
    }
    
    location @drupal {
    rewrite ^/(.*)$  /index.php?q=$1 last;
    }
    
    location /xf/ {
        try_files $uri $uri/ /index.php;
    }
    
    ## what do we do with php files
        location ~ \.php$ {
            include fastcgi_params;
            include fastcgi_config;
        }
    
    ## Disable viewing .htaccess & .htpassword
        location ~ /\.ht {
            deny  all;
        }
    }
    upstream backend {
            server 127.0.0.1:9000;
    }
    
     
  13. Blandt

    Blandt Well-Known Member

    Ok your nginx is in an infinite loop.

    Give me few minutes. I'm looking up documentation.
     
  14. Blandt

    Blandt Well-Known Member

    OK

    try this
    Code:
    server {
        listen   80;
        server_name  www.mydomain.net;
        rewrite ^/(.*) http://mydomain.net/$1 permanent;
    
           }
    
    server {
        listen  80;
        server_name mydomain.net;
        access_log  /var/log/nginx/mydomain.access.log;
    
    
    location / {
    	root   /home/mydomain/www;
            index index.html index.htm index.php;
            try_files $uri $uri/ @drupal;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root;
            include       fastcgi_params;
    
    	   }
    
    
    
    
    location /xf/ {
              try_files $uri $uri/ /xf/index.php;
                  }
    
    location ~ ^/xf/.+\.php$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root/xf/index.php
    include       fastcgi_params;
                             }
    
    
    
    	}
    
     
  15. Blandt

    Blandt Well-Known Member

    If this works for you, let me know so we can clean it up.
    It's messy this way... but we will clean it up ;)

    a
     
  16. Jesepi

    Jesepi Well-Known Member

    No, that didn't work at all. Everything was returning a "no input file specified" error. One thing I forgot about until just now are two included files in this config.

    The part:

    Code:
    ##what do we do with php files
        location ~ \.php$ {
        include fastcgi_params;
    	include fastcgi_config;
        }
    
    loads up the two following files:

    fastcgi_params
    Code:
    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_NAME        $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  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    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;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;
    
    fastcgi_config
    Code:
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_pass   backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/mydomain/www$fastcgi_script_name;
    #       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            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_intercept_errors        on;
            fastcgi_ignore_client_abort     off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
    
    Something that stands out is the

    Code:
     fastcgi_param  SCRIPT_FILENAME  /home/mydomain/www$fastcgi_script_name;
    and I am wondering if this is what is giving me problems. I've tried switching it with the commented line below it but I just get blank pages with no errors. That might actually be the key issue, yet I'm not knowledgeable enough with nginx to understand how to re-configure it.

    So as of the moment, I can do the following:
    • Access the "front" portion of the site, which is drupal and all it's SEO-friendly urls like mydomain.net/videos, mydomain.net/great-news-article, and mydomain.net/gallery/picture2
    • Access a vbulletin install in a subdirectory, /forums
    • Access the landing page for the XenForo install, clicking on any link that uses the forum software takes me to a blank drupal page. Unchecking "Use Full Friendly URLs" in Xenforo lets me use the software without issue.
     
    Blandt likes this.
  17. Blandt

    Blandt Well-Known Member

    Yes I was assuming you don't have a FastCGI config file because you have that one line.

    In any case ... ad this to your working config file

    location /xf/ { try_files $uri $uri/ /xf/index.php; }

    Note that nginx is looping because your root directory is where your drupal is installed, so when specifying xF condition you have to set the root directory for xF to work

    So ad a new location block for xF to your working config file :


    location /xf/ { try_files $uri $uri/ /xf/index.php; }
    includes your/path/to/your/customFastCGI/script/fastcgi_config

    Make sure nginx knows where you FastCGI configuration file is located. for example:
    /etc/nginx/conf/fastcgi_custom ...etc...
    Let me know
     
  18. Floren

    Floren Well-Known Member

    http://wiki.nginx.org/IfIsEvil

    Notice the name. :)

    The proper rewrite rules for Xen are:
    Code:
    location /xen/ {
    	try_files			$uri $uri/ /xen/index.php?$uri&$args;
    }
    
    location /xen/data/ {
    	allow				127.0.0.1;
    	deny				all;
    }
    
    location /xen/internal_data/ {
    	allow				127.0.0.1;
    	deny				all;
    }
    
    location /xen/library/ {
    	allow				127.0.0.1;
    	deny				all;
    }
    location ~ \.php$ {
    	fastcgi_index			index.php;
    	fastcgi_pass			127.0.0.1:9000;
    	include				fastcgi.conf;
    	fastcgi_intercept_errors	on;
    	fastcgi_ignore_client_abort	on;
    }
     
  19. Blandt

    Blandt Well-Known Member

    location /xen/ { try_files $uri $uri/ /xen/index.php?$uri&$args; }

    not necessary but if it works ...
     
  20. Luke F

    Luke F Well-Known Member

    Why would you want that?
     
    Blandt likes this.

Share This Page