Nginx没有重定向" /" to" /index.php"

时间:2017-05-27 22:29:25

标签: redirect nginx digital-ocean

Nginx没有改写" /"即使在/ etc / nginx / sites-available / default中添加重定向后,端点也会将index.php调用为默认值。

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

当我访问 my-site.com 时,我获得403 Forbidden,但它在 my-site.com/index.php 上正常工作,也是如此mysite.com/phpmyadmin/index.php 即可。此外,重定向似乎适用于index.html,但不适用于index.php。

的/ etc / nginx的/位点可用/默认

server {
    listen 80;
    listen [::]:80;
    server_name my-site.com www.my-site.com;
    return 301 https://$server_name$request_url;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        include snippets/ssl-my-site.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name my-site.com;

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

        location ~ [^/].php(/|$) {
                fastcgi_split_path_info ^(.+?.php)(/.*)$;
                if (!-f /var/www/html$fastcgi_script_name) {
                    return 404;
                }

                include fastcgi_params;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
                #fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_read_timeout 300;
    }

    location ~ /.ht {
        deny all;
    }

    location ~ /.well-known {
        allow all;
    }

    location /phpmyadmin {
        index index.php;
    }

    # Rewrite rules for WordPress Multi-site.
    if (!-e $request_filename) {
            rewrite /wp-admin$ $scheme://$host$uri/ permanent;
            rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
            rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
    }
}

# redirect www to non-www
server {
    listen 443; 
    listen [::]:443;
    server_name www.my-site.com;

    return 301 https://my-site.com$request_uri;
}

1 个答案:

答案 0 :(得分:0)

我最近遇到了同样的问题,这可能是解决方案:

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