仅在具有Nginx重写的root上从URL中删除index.php

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅在具有Nginx重写的root上从URL中删除index.php相关的知识,希望对你有一定的参考价值。

我正在使用Wordpress作为我的网站的根和Invision Power Boards作为论坛。

http://localhost -> Wordpress
http://localhost/forum -> IPB

我已经使用nginx-rewrite成功地从Wordpress URL中删除了“index.php”但是当我尝试在IPB上使用SEO友好URL时,nginx只返回到Wordpress的404页面。

我的配置是这样的:

#This removes "index.php" from Wordpress URLs
location / {
   index index.php index.html index.htm;
   try_files    $uri $uri/ /index.php?q=$uri&$args;
} 

然后我按照此链接修改我的nginx conf文件,以便能够使用IPB的SEO友好URL:http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/

#This part is to be able to use IPB SEO
location /forum/ {
    index index.php;
    try_files $uri $uri/ /forum/index.php?$uri&$args;
    rewrite ^ /index.php? last;
}

当我点击论坛上的链接(for example: http://localhost/forum/index.php/forum/51-sport/) nginx时,只需将我重定向到显示Wordpress 404错误页面的(http://localhost/forum/forum/51-sport/)

我对正则表达式知之甚少,所以任何帮助都会受到赞赏。


这是我修改后的整个conf文件,有点凌乱我接受了。

server {
    listen      80; ## listen for ipv4; this line is default and implied
    #listen     [::]:80 default ipv6only=on; ## listen for ipv6

    root    /home/user_name/public_html;

    access_log  /var/log/nginx/a/access.log;
    error_log  /var/log/nginx/a/error.log

    server_name localhost;
    server_tokens off;

    location / {
        try_files   $uri $uri/ @wordpress;
    }

    location @wordpress {
        fastcgi_pass php-fpm;
            fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
        fastcgi_param SCRIPT_NAME /index.php;
    }

    location /forum {
        try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
    }

    location /forum/ {
        try_files $uri $uri/ try_files $uri $uri/ /forum/index.php?q=$uri;
    }

    #location / {
        #index      index.php index.html index.htm;
        #try_files  $uri $uri/ /index.php?q=$uri&$args;
    #}

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

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

    location ~ .php$ {
        fastcgi_split_path_info ^(/)(/.*)$;
    }

    # Add trailing slash to */wp-admin and */forum requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9001
        #location ~ .php$ {
    #   fastcgi_split_path_info ^(/)(/.*)$;
    #   fastcgi_index   index.php;
        #       fastcgi_param SCRIPT_FILENAME /home/user_name/public_html$fastcgi_script_name;
        #       fastcgi_param PATH_INFO $fastcgi_script_name;
        #       include /etc/nginx/fastcgi_params;

        #REMOVE THIS        
        #fastcgi_read_timeout 60000;
        #fastcgi_send_timeout 6000;
        #}
}

自上一篇文章以来,我玩过IPB的SEO配置,我设法从URL中删除“index.php”。它当然不会影响结果。但似乎location /决定做什么,因此链接被视为Wordpress永久链接。


编辑 - 解决方案

    # Upstream to abstract backend connection(s) for php
upstream php {
#        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9001;
}

server {
        ## Your website name goes here.
        server_name localhost;
        ## Your only path reference.
        root /home/username/public_html;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

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

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

        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php;
        }

    location /forum {       
        try_files $uri $uri/ /forum/index.php;
        rewrite ^ /forum/index.php? break;
    }

    location ~ ^/forum/index.php {
        if ($args != "") {
            rewrite ^ http://www.google.com/ permanent;
        }
        try_files $uri $uri/ /forum/index.php;
        rewrite ^ /forum/index.php? last;
    }

    location /forum/admin/ {
        try_files $uri $uri/ /forum/admin/index.php;
        rewrite ^ /forum/admin/index.php? last;
    }



        location ~ .php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include /etc/nginx/fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}
答案

我也在nginx上使用wordpress和ipb,现在正在配置,我在ipb配置中添加了wordpress永久链接并打开seo重写URL,强制友好URL

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

它对我有用

以上是关于仅在具有Nginx重写的root上从URL中删除index.php的主要内容,如果未能解决你的问题,请参考以下文章

Nginx之反向代理日志格式集群缓存压缩URl 重写,读写分离配置

怎么在windows 下nginx路径重写

nginx 重写URL尾部斜杠

重写正则表达式,至少有一个字符的 URL

如何在 Nginx Regex 中重写 URL 并进行调试?

nginx地址重写模块