如何使用Nginx用斜杠分隔变量 [英] How to use Nginx to separate variables with slashes

查看:295
本文介绍了如何使用Nginx用斜杠分隔变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用

www.example.com/profiles/1234567890

代替

www.example.com/profiles?id=1234567890

但无法弄清楚该怎么做.

but was unable to figure out how to do so.

我在这里找到了等效的apache:将斜杠转换为变量后将文本使用HTACCESS ,但是我不知道如何在Nginx中使用它.我只需要一个变量id.

I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.

此外,是否可以进行这样的重写

Additionally, is it possible to do a rewrite such that

www.example.com/id/1234567890

指向

www.example.com/profiles/1234567890

但是网址没有改变吗?

server {
    server_name www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    listen 80;
    server_name domain.com;
    root /var/www/domain.com/public;
    index index.php;

    access_log  /var/www/domain.com/access.log;
    error_log  /var/www/domain.com/error.log;

    rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
    rewrite ^/id/(.*)$ /profiles/$1 last;

    # unless the request is for a valid file, send to bootstrap
    if (!-e $request_filename)
    {
        #rewrite ^(.+)$ /index.php?q=domain.com last;
        rewrite ^(.*)$ $1.php last;
    }

    # catch all
    error_page 404 /index.php;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
        access_log off;
    }

    location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
               try_files $uri =404;
               root /usr/share/;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include /etc/nginx/fastcgi_params;
        }
        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
        }
    }
    location /phpMyAdmin {
        rewrite ^/* /phpmyadmin last;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    ## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny  all;
    }

    location / {
                # This is cool because no php is touched for static content. 
                # include the "?$args" part so non-default permalinks doesn't break when using query string                
                try_files $uri $uri/ @extensionless-php?$args;    
        }

    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

    # use fastcgi for all php files
    location ~ \.php$
    {
        try_files $uri =404;

        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;

        # Some default config
        fastcgi_connect_timeout        20;
        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;
        fastcgi_intercept_errors    on;
        fastcgi_ignore_client_abort off;

    }
}

推荐答案

对于任何寻求答案的人,这都有效:

For anyone looking for the answer, this worked:

rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;

这篇关于如何使用Nginx用斜杠分隔变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆