如何设置php从Nginx中的单个位置运行


How to setup php to run from a single location in Nginx

我已经为此挣扎了一段时间。我想设置一个Wordpress博客,从服务器上的"/blogname"路径运行,而不是从根目录运行。我还希望路径的名称与Wordpress脚本所在的目录不同,因为服务器本身将运行django。

我将Nginx作为反向代理,并设置php-fpm来运行wordpress。这是我的Nginx配置文件:

http {
    include             mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;
    #tcp_nopush         on;
    #gzip               on;
    server {
        root         /Users/username/Dev/Wordpress/;
        index        index.php index.html index.htm;
        listen       8080;
        server_name  localhost;
        # Do not serve hidden files
        location ~ /'. {
            access_log      off; 
            log_not_found   off; 
            deny            all; 
        }
        # Static files
        location ~* '.(?:ico|css|js|gif|jpe?g|png)$ {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
        # This is the problem
        location /blogname {
            try_files $uri $uri/ /index.php;
            rewrite  /blogname(.*) /blog$1 last;
            fastcgi_index index.php;
            fastcgi_pass  127.0.0.1:9000;
            include       fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
    }
}

现在,当我访问localhost:8080/blogname时,我只是下载index.php脚本,而不是执行它

也欢迎其他提示。

更换此

location /blogname {
  try_files $uri $uri/ /index.php;
  rewrite  /blogname(.*) /blog$1 last;
  fastcgi_index index.php;
  fastcgi_pass  127.0.0.1:9000;
  include       fastcgi_params;
  fastcgi_param SCRIPT_FILENAME /Users/username/Dev/Wordpress/blog$fastcgi_script_name;
  fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

用这个

location ~ '.php$ {
  try_files                 $uri =404;
  fastcgi_pass              127.0.0.1:9000;
  fastcgi_index             index.php;
  fastcgi_intercept_errors  on;
  fastcgi_split_path_info   ^(.+'.php)(/.*)$;
  include                   fastcgi.conf;
}

我希望这对你有用,因为这是我在本地服务器上使用的。