简体   繁体   中英

nginx - Rewriting wrong url values

Hi I'm having much trouble getting my site to work with nginx. My site is based on Yii framework and clean url format is enabled.

An instance of the problem is with the search bar. The action url for the search form is /Site/index.php/real/search . Upon searching i'm being redirected to http://localhost/Site/index.php/index.php/real/view/id/4 . Now here is the problem. Nginx is somehow adding an extra index.php in the url.

Thus the page doesn't load any css or js and is being displayed in plain html with huge number of errors. The actual url of the page is http://localhost/Site/index.php/real/view/id/4 .

Here is the nginx server config named default.conf

server { 
    listen 80; 
    server_name localhost:80; 
    root /site; 

    access_log /site/access_log.log;
    error_log /site/error_log.log;

    index index.php;
    client_max_body_size 1000M; 
    default_type text/html;
    charset utf-8;

    location = /favicon.ico {
        try_files /favicon.ico =204;
    }

    ## The main location is accessed using Basic Auth.
    location / {

        ## Use PATH_INFO for translating the requests to the
        ## FastCGI. This config follows Igor's suggestion here:
        ## http://forum.nginx.org/read.php?2,124378,124582.
        ## This is preferable to using:
        ## fastcgi_split_path_info ^(.+\.php)(.*)$
        ## It saves one regex in the location. Hence it's faster.
        location ~ ^(?<script>.+\.php)(?<path_info>.*)$ {
            include fastcgi_params;
            ## The fastcgi_params must be redefined from the ones
            ## given in fastcgi.conf. No longer standard names
            ## but arbitrary: named patterns in regex.
            fastcgi_param SCRIPT_FILENAME $document_root$script;
            fastcgi_param SCRIPT_NAME $script;
            fastcgi_param PATH_INFO $path_info;
            ## Passing the request upstream to the FastCGI
            ## listener.
            fastcgi_pass 127.0.0.1:9000;
        }

        ## Protect these locations. Replicating the .htaccess
        ## rules throughout the chive distro.
        location /protected {
            internal;
        }

        location /framework {
            internal;
        }

        ## Static file handling.
        location ~* .+\.(?:css|gif|htc|js|jpe?g|png|swf)$ {
            expires max;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=100 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
    }

    ## We need to capture the case where the index.php is missing,
    ## hence we drop out of the path info thingie.
    location ~* /([^\.])$ {
        return 302 /index.php/$1;
    }

    ## Close up git repo access.
    location ^~ /.git {
        return 404;
    }

}

Please let me know how can i get nginx to rewrite the correct url, i've been trying to find a correct configuration for weeks now without any success. I will appreciate any help.

Thanks, Maxx

Why didn't you add showScriptName => false? Here is my configuration

Nginx config for yii framework

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    access_log  /var/log/nginx/access.log;

    sendfile        on;
    tcp_nodelay  on;
    keepalive_timeout  65;
    gzip_disable        "MSIE [1-6]\.(?!.*SV1)";
    gzip_vary           on;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_comp_level     6;
    gzip_buffers        16 8k;
    gzip_http_version   1.1;
    gzip_types          text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    charset             utf-8;
    client_max_body_size 100m;
    fastcgi_read_timeout 300;

    server {
    listen       80;
    root   /var/www/mysite/frontend/web;
    server_name  mysite.loc;
    error_log /var/log/nginx/mysite-error.log;
    access_log /var/log/nginx/mysite-access.log;
    set $yii_bootstrap "index.php";

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

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

    location ~ \.php$ {
    set $fsn /$yii_bootstrap;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include fastcgi_params;
        fastcgi_intercept_errors        on;

        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
    }
    }
}

fastcgi_pass can be different for example localhost:9000, localhost:9001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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