现有站点的 nginx 子目录中的 symfony 应用程序 [英] symfony app in a subdirectory in nginx for an existing site

查看:30
本文介绍了现有站点的 nginx 子目录中的 symfony 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 symfony 为现有站点开发我的 API.但我无法像

I'm use symfony develop my API for an existing site. but i can't configure my symfony app like

www.domain.com/sfapi/xxx
www.domain.com/sfapi/xxxxx

这里是我的 nginx vhost 配置

here my nginx vhost configure

/usr/local/nginx/conf/sfapi.conf 用于 symfony 应用程序配置.

/usr/local/nginx/conf/sfapi.conf for symfony app configure.

location @rewriteapp {
          if (-f $request_filename) {
                  break;
          }
          rewrite ^/(.*)$ /sfapi/app.php/$1 last;
   }

   location ^~ /sfapi/ {
           alias /home/wwwroot/www.domain.com/wuye/chengshi/web;
           #root /home/wwwroot/www.domain.com/wuye/chengshi/web;
           index app.php;
           set $root "/home/wwwroot/www.domain.com/wuye/chengshi/web";
           # try to serve file directly, fallback to app.php
           try_files $uri  @rewriteapp;
   }
   # PROD
   location ~ ^/app\.php(/|$) {
          fastcgi_pass  unix:/tmp/php-cgi.sock;
          fastcgi_split_path_info ^(.+\.php)(/.*)$;

           include fastcgi_params;
           fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
           fastcgi_param SCRIPT_NAME $fastcgi_script_name;
           fastcgi_param PATH_INFO $fastcgi_path_info;
   }

主虚拟主机配置

server {
        listen 80;
        listen [::]:80;
        server_name chengshi.91zhangyu.com;
        rewrite ^(.*) https://$server_name$1 permanent;
}
server{
        listen 443;
        #listen 80;
        listen [::]:443;
        server_name www.domain.com;
        index index.html index.htm app.php index.php default.html default.htm default.php;
        ssl on;
        ssl_certificate   /home/certificate/www.domain.com.crt;
        ssl_certificate_key  /home/certificate/www.domain.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        root  /home/wwwroot/www.domain.com/chengshi/chengshi;

        include chengshi.wuye.conf;
        include destoon.conf;
        #error_page   404   /404.html;
        location ~ [^/]\.php(/|$)
        {
            # comment try_files $uri =404; to enable pathinfo
            #try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            include pathinfo.conf;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        access_log /home/wwwlogs/www.domain.com access;
    }

destoon.conf

destoon.conf

rewrite ^/(.*)\.(asp|aspx|asa|asax|dll|jsp|cgi|fcgi|pl)(.*)$ /404.php last;
rewrite ^/(.*)/file/(.*)\.php(.*)$ /404.php last;
rewrite ^/(.*)-htm-(.*)$ /$1.php?$2 last;
rewrite ^/(.*)/show-([0-9]+)([\-])?([0-9]+)?\.html$ /$1/show.php?itemid=$2&page=$4 last;
rewrite ^/(.*)/list-([0-9]+)([\-])?([0-9]+)?\.html$ /$1/list.php?catid=$2&page=$4 last;
rewrite ^/(.*)/show/([0-9]+)/([0-9]+)?([/])?$ /$1/show.php?itemid=$2&page=$3 last;
rewrite ^/(.*)/list/([0-9]+)/([0-9]+)?([/])?(.*)?$ /$1/list.php?catid=$2&page=$3&attr=$5 last;
rewrite ^/(.*)/([A-za-z0-9_\-]+)-c([0-9]+)-([0-9]+)\.html$ /$1/list.php?catid=$3&page=$4 last;
rewrite ^(.*)/([a-z]+)/(.*)\.shtml$ $1/$2/index.php?rewrite=$3 last;
rewrite ^/(com)/([a-z0-9_\-]+)/([a-z]+)/(.*)\.html$ /index.php?homepage=$2&file=$3&rewrite=$4 last;
rewrite ^/(com)/([a-z0-9_\-]+)/([a-z]+)([/])?$ /index.php?homepage=$2&file=$3 last;
rewrite ^/(com)/([a-z0-9_\-]+)([/])?$ /index.php?homepage=$2 last;

fastcgi.conf

fastcgi.conf

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  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;

路径信息.conf

fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO       $path_info;
try_files $fastcgi_script_name =404;

nginx_error.log
2017/10/17 01:08:34 [错误] 20371#0:*543 重写或内部重定向循环,同时重定向到命名位置@rewriteapp",

nginx_error.log
2017/10/17 01:08:34 [error] 20371#0: *543 rewrite or internal redirection cycle while redirect to named location "@rewriteapp",

谢谢

推荐答案

您的重写导致循环回到 /sfapi/ 位置块.

Your rewrite is causing a loop back to the /sfapi/ location block.

尝试像这样删除 @rewriteapp 块:

Try removing the @rewriteapp block like this:

   location ^~ /sfapi/ {
           alias /home/wwwroot/www.domain.com/wuye/chengshi/web;
           #root /home/wwwroot/www.domain.com/wuye/chengshi/web;
           index app.php;
           set $root "/home/wwwroot/www.domain.com/wuye/chengshi/web";
           # try to serve file directly, fallback to app.php
           try_files $uri /sfapi/app.php/$1 last;
   }

这篇关于现有站点的 nginx 子目录中的 symfony 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆