简体   繁体   English

仅在 root 上使用 Nginx 重写从 URL 中删除 index.php

[英]Remove index.php from URL only on root with Nginx rewrite

I'm using Wordpress as root of my website and Invision Power Boards as forum.我使用 Wordpress 作为我网站的根目录,使用 Invision Power Boards 作为论坛。

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

I have removed "index.php" from Wordpress URLs successfully with Nginx-rewrite however when I try to use SEO Friendly URLs on IPB, nginx simply returns to Wordpress' 404 page.我已经使用 Nginx 重写成功地从 Wordpress URL 中删除了“index.php”,但是当我尝试在 IPB 上使用 SEO 友好 URL 时,nginx 只是返回到 Wordpress 的 404 页面。

My configuration is like this:我的配置是这样的:

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

Then I follow this link to modify my nginx conf file in order to be able to use SEO friendly URLs of IPB: http://www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-and-nginx/然后我按照这个链接修改我的 nginx conf 文件,以便能够使用 IPB 的 SEO 友好 URL: http : //www.devcu.com/forums/topic/262-furl-friendly-urls-with-ipb-和-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;
}

When I click a link on my forum (for example: http://localhost/forum/index.php/forum/51-sport/) nginx simply redirects me to (http://localhost/forum/forum/51-sport/) which displays Wordpress 404 error page.当我单击论坛上的链接(for example: http://localhost/forum/index.php/forum/51-sport/) nginx 只是将我重定向到(http://localhost/forum/forum/51-sport/)显示 Wordpress 404 错误页面。

I have very little knowledge about regex so any help would be appreciated.我对正则表达式知之甚少,因此将不胜感激。


This is my whole conf file after modifications, little messy I accept that.这是我修改后的整个 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;
        #}
}

Since the last post, I have played with IPB's SEO configurations and I managed to remove "index.php" from URLs.自从上一篇文章以来,我一直在使用 IPB 的 SEO 配置,并设法从 URL 中删除了“index.php”。 It doesn't effect the result of course.当然不影响结果。 But it seems that location / decides what to do and therefore link is being considered as a Wordpress permalink.但似乎location /决定要做什么,因此链接被视为 Wordpress 永久链接。


EDIT - Solution编辑 - 解决方案

    # 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;
        }
}

I'm too using wordpress and ipb on nginx, now configuring, i added wordpress permalink to ipb config and turn on seo Rewrite URLs, Force Friendly URLs我也在 nginx 上使用 wordpress 和 ipb,现在正在配置,我将 wordpress 永久链接添加到 ipb 配置并打开 seo Rewrite URLs,Force Friendly URLs

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

it worked for me它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM