yii2高级的nginx配置

时间:2018-07-24 06:23:27

标签: php nginx yii2

我正在为具有三个子项目的单个域yii2应用程序配置nginx:后端,前端和api。我设法为后端和前端子项目应用了一个github示例(下面作为配置1提供),但是我在使/ api子项目运行时遇到了麻烦。我已经尝试了解决方案 https://stackoverflow.com/questions/43045477/nginx-config-for-multiple-yii2-advanced-project-locations-deploy-development等,但没有蛋糕。

到目前为止,我设法提供的配置正确呈现了server/api或''规则,但是对于任何server/api/**的URL(其他URL管理器规则)返回404。

我不是100%确定错误是否出在nginx部分或其配置错误的yii应用程序上,但是我仍然要怪nginx,因为配置2会使api按预期工作(前端和后端损坏)。 / p>

任何提示或指示都将不胜感激!

配置1:

server {
    listen 80;
    server_name test.com;

    set $base_root /var/www/html;
    root $base_root;

    #error_log /var/log/nginx/dev.error.log warn;
    #access_log /var/log/nginx/dev.access.log main;
    charset UTF-8;
    index index.php;

    #frontend
    location / {
        root $base_root/frontend/web;
        try_files $uri $uri/ /frontend/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /admin {
        alias $base_root/backend/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /admin/ {
        #    return 301 /admin;
        #}

        # if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /backend/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/admin/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/admin/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /api {
        alias $base_root/api/web/;

        try_files $uri $uri/ /api/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        location ~ ^/api/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
            log_not_found off;
            access_log off;
            try_files $uri =404;
        }

        location ~ ^/api/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location ~ ^/.+\.php(/|$) {
        rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
        rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;
        rewrite (?!^/api/web)^/api(/.+)$ /api/web$1 break;

        fastcgi_pass php:9000; # proxy requests to a TCP socket
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        try_files $fastcgi_script_name =404;
    }

    location ~ /\. {
        deny all;
    }
}

配置2:

server {
    listen 80;
    index index.php;

    server_name test.com;

    client_max_body_size 32m;
    client_body_buffer_size 32m;

    charset utf-8;

    gzip on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    rewrite (.+)/$ $1 redirect;

    set $base_root /var/www/html;    
    set $base_path backend;

    if ( $host ~ ^admin ) {
        set $base_path backend;
    }
    if ( $host ~ ^api ) {
        set $base_path api;
    }

    root $base_root/$base_path/web;

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        try_files $uri =404;
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
        try_files $uri =404;
    }

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ \.php$ {
        include fastcgi_params;

        fastcgi_pass php:9000;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SERVER_NAME $host;

        internal;
    }

    location =  /robots.txt     { access_log off; log_not_found off; }
    location =  /favicon.ico    { access_log off; log_not_found off; }
    location ~* /CHANGELOG      { access_log off; log_not_found off; deny all; }
    location ~* /LICENSE        { access_log off; log_not_found off; deny all; }
    location ~* /README         { access_log off; log_not_found off; deny all; }
    location ~* /\.             { access_log off; log_not_found off; deny all; }
}

api / main.php:

'homeUrl' => '/api/',
    'components' => [
        'request' => [
            'baseUrl' => '/api',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/api/index.php',
            'rules' => [
                '' => 'site/index',
                'GET,HEAD status/get' => '/status/get',

     ...

0 个答案:

没有答案