programing

Nginx rewrite를 사용하여 URL에서 index.php 제거

javajsp 2023. 11. 5. 11:02

Nginx rewrite를 사용하여 URL에서 index.php 제거

저는 웹사이트의 루트로 워드프레스를 사용하고 있고 포럼으로 인비전 파워 보드를 사용하고 있습니다.

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

나는 Nginx-rewrite로 워드프레스 URL에서 "index.php"를 성공적으로 제거했지만 IPB에서 SEO Friendly URL을 사용하려고 하면 nginx는 워드프레스의 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;
} 

그런 다음 이 링크를 따라 IPB의 SEO 친화적인 URL을 사용할 수 있도록 nginx conf 파일을 수정합니다. 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는 간단히 다음으로 리디렉션합니다.(http://localhost/forum/forum/51-sport/)Wordpress 404 오류 페이지가 표시됩니다.

저는 regex에 대한 지식이 거의 없으니 어떤 도움이라도 주시면 감사하겠습니다.


이것은 수정 후에 내 전체 컨프 파일입니다. 약간 지저분합니다.

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 /할 일을 결정하므로 링크는 워드프레스 퍼말링크로 간주됩니다.


편집 - 솔루션

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

wordpress와 ipbon nginx를 너무 사용하고 있어요, 이제 구성합니다, ipb config에 wordpress permalink를 추가하고 seo 다시 쓰기 URL, Force Friendly URL을 설정합니다.

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

그것은 나에게 효과가 있었다.

언급URL : https://stackoverflow.com/questions/10591258/remove-index-php-from-url-only-on-root-with-nginx-rewrite