大网数据广告活动
服务器运维问答

How to rewrite and parse a single file with .js extension as php in Nginx

· 52CE 我爱测

My request looks like this domain.com/lib/browser/detect.js Now internally I would like to serve /lib/browser/detect.php My Nginx conf looks like this location = /lib/browser/detect.js { try_files non-existent /lib/browser/detect…

问题原文

原帖链接 · Aley · CC BY-SA 4.0

My request looks like this

domain.com/lib/browser/detect.js

Now internally I would like to serve /lib/browser/detect.php

My Nginx conf looks like this

location = /lib/browser/detect.js {
        try_files non-existent /lib/browser/detect.php;
}

location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*);
        fastcgi_read_timeout 900s;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Nginx forces my browser to download this file.

已采纳解答

原帖链接 · virullius · CC BY-SA 4.0

Try using the rewrite directive instead of try_files as that is meant to server static files only.

Something like this might do it:

location = /lib/browser/detect.js {
    rewrite .* /lib/browser/detect.php;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+?\.php)(/.*);
    fastcgi_read_timeout 900s;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

The rewrite in this case can probably match on anything since it's already inside an exact match location block. You could also make the match in the rewrite directive and take it out of the location block and into the server block like I've done in my test here:

server {
  listen 80 default_server;
  root /var/www/;
  rewrite /detect.js /detect.php;
  location / {
     try_files $uri $uri/ =404;
  }
  location ~ \.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass 127.0.0.1:9000;
  }
}

This is otherwise default config from nginx and php-fpm packages on alpine linux. Contents of fastcgi.conf file are:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。