nginx & config for permalinks 不起作用;改为下载 PHP 文件



我已经安装了nginx,我尝试在其中运行wordpress。一切正常,除了永久链接。

这是我正在使用的vhost文件:

server {
listen 123456:80;
server_name my-domain.com;
if ($host ~* www.(.*)) {
    set $wwwless $1;
    rewrite ^(.*)$ $scheme://$wwwless$1 permanent;
}
root /var/www/my-folder;
index index.php;
    location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

(我已经用my-domain.com,my-folter和ip 123456替换了上述代码中的关键数据。)

index.php,admin-panel和使用标准链接(.../?p = 123)工作。如果我启用一些永久链接,则index.php和管理面板仍然可以工作。但是,如果我尝试打开WordPress博客的另一个网站,我的浏览器将下载index.php :(

为.php位置配置:

尝试这样的事情
location ~ .php$ {
    include     /etc/nginx/fastcgi_params;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

这是我的WordPress配置适合您的WordPress配置。

server { # the redirecting from www to non-www
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
  listen  80;
  server_name example.com;
  root /path/to/root;
  # make sure the directory /var/log/nginx exists
  access_log /var/log/nginx/wordpress_access.log;
  error_log /var/log/nginx/wordpress_error.log;
  index index.php;
  location / {
    try_files $uri /index.php$request_uri$is_args$query_string;
  }
  location ~ .php {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
  location ~ /.ht { # avoid downloading htaccess, htpasswd, etc files
    deny all;
  }
}

您可以尝试此VHOST文件

server {
   listen 80;
   server_name www.example.com example.com;
   root /var/www/www.example.com/web;
   if ($http_host != "www.example.com") {
             rewrite ^ http://www.example.com$request_uri permanent;
   }
   index index.php index.html;
   location = /favicon.ico {
            log_not_found off;
            access_log off;
   }
   location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
   }
   # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
   location ~ /. {
            deny all;
            access_log off;
            log_not_found off;
   }
   location / {
            try_files $uri $uri/ /index.php?$args;
   }
   # Add trailing slash to */wp-admin requests.
   rewrite /wp-admin$ $scheme://$host$uri/ permanent;
   location ~*  .(jpg|jpeg|png|gif|css|js|ico)$ {
            expires max;
            log_not_found off;
   }
   location ~ .php$ {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }

}

最新更新