symfony app in a subdirectory in nginx for an exis

2019-09-18 15:06发布

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

here my nginx vhost configure

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

main vhost conf

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

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

pathinfo.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 [error] 20371#0: *543 rewrite or internal redirection cycle while redirect to named location "@rewriteapp",

thanks

1条回答
虎瘦雄心在
2楼-- · 2019-09-18 15:22

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

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;
   }
查看更多
登录 后发表回答