dongtu4028 2018-10-06 13:39
浏览 80

Nginx不会重写好的网址,而是下载php文件

I am trying to set nice urls for nginx. I want to rewrite for example example.com/account.php to example.com/dashboard with

    location =  /dashboard {
    rewrite ^dashboard/?$ /acc0unt.php last;
}

But that does not work. Nginx downloads the file instead. Full nginx config:

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri; # port 80 default

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server { # port 80 default
        access_log /var/log/nginx/example.com.access.log; 
        error_log /var/log/nginx/example.com.error.log;
        root /var/www/example.com/; # default directory where the files will be stored and served from
        index index.php index.html index.htm; # index defined to be served under directory
        server_name example.com; # name of the virtual host or domain
    location / {
        # URLs to attempt, including pretty ones.
        try_files $uri $uri/ /index.php?$args;
    }
        error_page 404 500 502 503 504 /404.php;
        location = /404.php {
              root /var/www/example.com/;
        }
    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }
        # Serve PHP scripts to FastCGI server our php-fpm server listening on 127.0.0.1:9000
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass 127.0.0.1:9000;
                include fastcgi_params;
        }
    include common/locations.conf;

# redirects start
rewrite ^/signup-([0-9]+).php$ https://example.com/recruit.php?ref=$1 permanent;
rewrite ^/([0-9]+)-([a-z]+)-([0-9]+).php$ https://example.com/idevads.php?id=$1&ad=$3 permanent;
rewrite ^/([0-9]+)-([a-z]+)-([0-9]+)-([0-9]+).php$ https://example.com/idevads.php?id=$1&ad=$3&page=$4 permanent;
rewrite ^/([0-9]+).php$ https://example.com/promo.php?id=$1 permanent;
rewrite ^/([0-9]+)-([0-9]+).php$ https://example.com/promo.php?id=$1&page=$2 permanent;
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+).php$ https://example.com/promo.php?id=$1&page=$2&set=$3 permanent;
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+).php$ https://example.com/promo.php?id=$1&page=$2&set=$3&link=$4 permanent;
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+).php$ https://example.com/promo.php?id=$1&page=$2&set=$3&link=$4&keyword=$5 permanent;
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+)-([0-9]+).php$ https://example.com/promo.php?id=$1&page=$2&set=$3&link=$4&keyword=$5&custom=$6 permanent;
rewrite ^/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([a-zA-Z]+)-([0-9]+)-([0-9]+).php$ https://example.com/promo.php?id=$1&page=$2&set=$3&link=$4&keyword=$5&custom=$6&url=$7 permanent;
# redirects end
#rewrites start
//rewrite php files to dir names here
#rewrites end



    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen   0.0.0.0:80;
        server_name example.com;
    return 404; # managed by Certbot


}server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name www.example.com;
    listen   0.0.0.0:80;
    return 404; # managed by Certbot


}

fastcgi-php.conf snippet is:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

Any ideas how to solve this? I want to serve /dashboard and run acc0unt.php for example. I read some of the posts around here and none really helped.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘