在NGiNX中重写文件扩展名

我正在寻找像domain.com/login而不是domain.com/login.php文件

我的NGiNX站点configuration是:

#02API server { # listen 80 deferred; # for Linux # listen 80 accept_filter=httpready; # for FreeBSD listen 80; # The host name to respond to server_name api.api.com; # Path for static files root /app/api/api; # Try static files first, then php index index.html index.htm index.php; # Specific logs for this vhost access_log /app/api/log/log-api-access.log; error_log /app/api/log/log-api-error.log error; #Specify a charset charset utf-8; # Redirect needed to "hide" index.php location / { try_files $uri $uri/ /index.php?q=$uri&$args; } # Don't log robots.txt or favicon.ico files location ~* ^/(favicon.ico|robots.txt)$ { access_log off; log_not_found off; } # Custom 404 page error_page 404 /404.html; location ~* ^.+.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv|xml) { access_log off; expires 30d; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_intercept_errors on; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Deny access to .htaccess location ~ /\.ht { deny all; } # Include the basic h5bp config set include h5bp/basic.conf; } 

但每次我尝试:domain.com/login

该文件被下载,但domain.com/login.php obviusly显示的PHP文件的内容。

有没有办法解决这个问题?

谢谢

如果要使用try_files参数将.php添加到请求的URL的末尾,请尝试将try_files行replace为:

 try_files $uri $uri/ $uri.php?$args; 

或者,您可以为要重写的每个单独的URL添加重写,但这可能会变得混乱,具体取决于您要为此设置多less个文件。