nginx clean url with seo friendly file names

2019-01-20 06:37发布

I wish to implement the following which is working perfectly under Apache. This is being done for better SEO of the URLs.

Example URLS:

http://www.astrogyan.com/enter/indian_astrology_horoscope_chart_prediction.html
http://www.astrogyan.com/know_your_gemstone/gID-7/sani_planet_saturn_gemstone_blue_sapphire_neelam.html

What I am exactly looking forward to is a location regex to catch all extension-less php scripts in the ROOT FOLDER ONLY for processing by php-fptm.

In all the above URLs "enter", "know_your_gemstone" are all PHP scripts and what follows them are dummy file names generated by PHP for SEO. Actually "indian_astrology_horoscope_chart_prediction.html" file name does not exist. In Apache, I use the following which intercepts "enter / know_your_gemstone" etc and never bothers about the rest of the file name:

DefaultType application/x-httpd-php

In the last of the above URL, "gID-7" is used to pass a variable to the script to show appropriate content. While this URL is showing DYNAMIC content, the URL is so crafted that is looks like a STATIC URL which can be indexed by search engines easily. This variable parsing is done in PHP already and has nothing to do with Nginx. I beleive this part is already referred as pretty url / clean url.

I need to know how best can this be implemented under NGINX? I need the regex to process all scripts (extension less files) in ROOT FOLDER and ignore what follows after such script names. If such a file does not exist, then consider to check the rest of the URL hoping it to be a valid directory followed by a file name. This directory portion is optional and not essential to my present needs.

I have a VPS running ubuntu where I have installed nginx with php-fpm and for normal URL's like index.htm / index.php the setup is working fine. I am not a pro in regex writing hence I am stuck up at this juncture. I searched online under many nginx blogs / forum but could not find the right solution.

I am using the latest development version of Nginx v1.1.17 with php v5.3.6.13. I have also compiled additional modules like more header, cache purge, memcache etc.

Any help on this will be most appreciated. Thanks in advance...

标签: php nginx
2条回答
forever°为你锁心
2楼-- · 2019-01-20 06:52

This should work for you:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            fastcgi_pass   127.0.0.1:8080;
            ...
        }
    }
}

Alternative with proxy:

server {
    listen 80;
    server_name example.com;
    root   /full/server/path/to/your/cms;
    index  index.php;

    location / {
        try_files $uri $uri/ /phphandler
    }

    location /phphandler {
        internal;
        # nested location to filter out static items not found
        location ~ .php$ {
            rewrite ^/([^/]*)(.*) /$1 break;
            proxy_pass   127.0.0.1:8080;
            ...
        }
    }
}
查看更多
Bombasti
3楼-- · 2019-01-20 07:06

You are referring to mod_rewrite, that is commonly used by CMS to rewrite a seemingly static file URL into an index.php dynamic URL. This can be done in nginx, also via rewrite:

server {
    # port, server name, log config omitted

    location / {
        root   /path/to/your/cms;
        index  index.php index.html;  # replace index.php with your real handler php

        if (!-f $request_filename) {  # request is not a file
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
        if (!-d $request_filename) {  # request is not a dir
            rewrite  ^(.*)$  /index.php?q=$1  last;
            break;
        }
    }

    # sample fastcgi php config, to allow running of your index.php
    location ~ .php$ {
      fastcgi_pass   127.0.0.1:9090;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  /path/to/your/cms$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;
    }

    # static files
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
        access_log        off;
        expires           30d;
    }

    error_page  404              /index.php;
 }
查看更多
登录 后发表回答