Hướng dẫn nginx remove index php from url - nginx xóa chỉ mục php khỏi url

Tôi đang sử dụng WordPress làm Root của trang web và bảng sức mạnh Invision của tôi làm diễn đàn.

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

Tôi đã xóa "index.php" khỏi URL WordPress thành công với nginx-rewrite tuy nhiên khi tôi cố gắng sử dụng các URL thân thiện với SEO trên IPB, nginx chỉ cần quay lại trang 404 của WordPress.

Cấu hình của tôi là như thế này:

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

Sau đó, tôi theo liên kết này để sửa đổi tệp nginx conf của tôi để có thể sử dụng URL thân thiện với SEO của IPB: http://www.devcu.com/forums/topic/262-furl-frielly-urls-with-ipb- và 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;
}

Khi tôi nhấp vào một liên kết trên diễn đàn của mình (for example: http://localhost/forum/index.php/forum/51-sport/) Nginx chỉ cần chuyển hướng tôi đến (http://localhost/forum/forum/51-sport/) hiển thị trang lỗi WordPress 404.

Tôi có rất ít kiến ​​thức về Regex vì vậy bất kỳ sự giúp đỡ sẽ được đánh giá cao.


Đây là toàn bộ tệp conf của tôi sau khi sửa đổi, tôi ít lộn xộn tôi chấp nhận điều đó.

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

Kể từ bài đăng cuối cùng, tôi đã chơi với các cấu hình SEO của IPB và tôi đã xoay sở để xóa "index.php" khỏi URL. Nó không ảnh hưởng đến kết quả của tất nhiên. Nhưng dường như location / quyết định phải làm gì và do đó liên kết đang được coi là một permalink WordPress.


Chỉnh sửa - Giải pháp

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