nginx嵌套子文件夹,可在virtualhost中使用

时间:2018-09-25 15:50:09

标签: nginx virtualhost subdirectory

我正在尝试在服务器上运行2个应用程序...

  • 根文档(wordpress)上的第一个应用程序
  • 以及文件夹/ app中的第二个应用程序(symfony4)

问题是我找不到在嵌套文件夹上使用别名的方法... 我的配置文件:

server {
  listen 80;

    server_name ~^(?<folder>[^.]*).magana.dev.hexis.fr;


    charset utf-8;
    index index.php index.html index.htm;

    root /var/www/projects/dev/magana/$folder/htdocs;
    access_log /var/www/projects/dev/magana/$folder/access.log;
    error_log  /var/www/projects/dev/commons/logs/magana_error.log;

    client_max_body_size 200M;

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|css)$ {
        access_log        off;
        log_not_found     off;
        expires           30d;
        add_header Pragma "public";
    }

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }


    location ~ \.php$ {            
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV magana;
        include fastcgi_params;
        fastcgi_read_timeout 600;
        fastcgi_buffers 16 16k; 
        fastcgi_buffer_size 32k;
        fastcgi_param SERVER_NAME $folder.magana.dev.hexis.fr;
   }
location /app {
        alias /var/www/projects/dev/magana/$folder/htdocs/app/public;
        try_files $uri $uri/ @app;

        location ~ \.php$ {
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    }

    location @app {
        rewrite /app/(.*)$ /app/public/index.php?/$1 last;
    }
   location ~ /\.ht {
        deny all;
    }
}

我想念的是什么? 谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

在此代码中,我可以访问应用程序,但仍然找不到声明e = kernelroot目录的方法

server {
  listen 80;

    server_name ~^(?<folder>[^.]*).magana.dev.example.fr;


    charset utf-8;
    index index.php index.html index.htm;

    set $symfonyRoot /var/www/projects/dev/magana/$folder/htdocs/app/public;
        set $symfonyScript index.php;

    root /var/www/projects/dev/magana/$folder/htdocs;
    access_log /var/www/projects/dev/magana/$folder/access.log;
    error_log  /var/www/projects/dev/commons/logs/magana_error.log;

    client_max_body_size 200M;

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp$
        access_log        off;
        log_not_found     off;
        expires           30d;
        add_header Pragma "public";
    }
    location /app {
                root $symfonyRoot;
                rewrite ^/app/(.*)$ /$1 break;
                try_files $uri @symfonyFront;
        }
    location @symfonyFront {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $symfonyRoot/$symfonyScript;
                fastcgi_param SCRIPT_NAME /app/$symfonyScript;
                fastcgi_param REQUEST_URI /app$uri?$args;
        }


    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV magana;
        include fastcgi_params;
        fastcgi_read_timeout 600;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SERVER_NAME $folder.magana.dev.hexis.fr;
    }

    location ~ /\.ht {
        deny all;
    }
}