【问题标题】:Yii2 - Nginx error with API location but not with ADMIN locationYii2 - API 位置的 Nginx 错误,但不是 ADMIN 位置
【发布时间】:2019-07-30 23:49:14
【问题描述】:

我的开源 Web 应用程序有问题,我尝试在我的 Web 应用程序中创建一个 API 应用程序,但是当我从浏览器访问它时,它从不调用“/api/web/index.php”文件。

它有效: "http://yii2-app-advanced.local/admin/site/login"

它不起作用: "http://yii2-app-advanced.local/api/customer/login"

我搜索了很多,做了很多测试,但没有成功。

这是我的 nginx 文件: https://github.com/prsolucoes/yii2-app-advanced/blob/module-api/extras/docker/nginx/conf.d/yii2-app-advanced.conf

这是存储库: https://github.com/prsolucoes/yii2-app-advanced/tree/module-api

我在nginx上放了一些日志:

URI: /api/customer/login
BOOTSTRAP: index.php
TRY: /api/web/index.php
DOC ROOT: /usr/share/nginx/html/api/web
ROOT PATH: /usr/share/nginx/html

一切似乎都是正确的,因为管理员运作良好并遵循相同的数据,仅将 api 更改为后端文件夹。

谢谢。

【问题讨论】:

  • 你能在这里添加你的路由文件,或者你的路由配置吗?
  • Stackoverflow 不要把所有的东西都放在这里,因为太长了。这是我的 nginx 文件:github.com/prsolucoes/yii2-app-advanced/blob/module-api/extras/…
  • @mrateb 我在 Yii2 中没有路线,它们是空的。 Nginx 发送给应用程序,他们决定去哪里。
  • 您好,问题没有解决。我不知道为什么位置“/api”不调用“/api/web/index.php”,而是停止它,我将文件夹重命名为“ws”,现在“/api”调用“/ws/web /index.php”。我不知道“别名”或“尝试”nginx 命令是否存在错误/问题。如果有人对此有所了解,请帮助我,因为我的意图是使用文件夹名称“api”。谢谢。

标签: php nginx yii2


【解决方案1】:
server {
    charset      utf-8;
    client_max_body_size  200M;
    listen       80; ## listen for ipv4
    #listen       [::]:80 default_server ipv6only=on; ## listen for ipv6
    server_name  site.com;
    root         /var/www/site.com;


    location / {
        root  /var/www/site.com/frontend/web;
        try_files  $uri /frontend/web/index.php?$args;
        # avoiding processing of calls to non-existing static files by Yii
        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
            access_log  off;
            expires  360d;
            try_files  $uri =404;
        }
    }

    location /api {
        root /var/www/site.com/api/web;
        try_files  $uri /api/web/index.php?$args;

        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
          access_log  off;
          expires  360d;
          try_files  $uri =404;
        }
    }

    location /admin {
        alias  /var/www/site.com/backend/web;
        rewrite  ^(/admin)/$ $1 permanent;
        try_files  $uri /backend/web/index.php?$args;
    }

    location ^~ /uploads {
       autoindex on;
    }

    # avoiding processing of calls to non-existing static files by Yii

    location ~ ^/admin/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
        access_log  off;
        expires  360d;
        rewrite  ^/admin/(.+)$ /backend/web/$1 break;
        rewrite  ^/admin/(.+)/(.+)$ /backend/web/$1/$2 break;
        try_files  $uri =404;
    }

    location ~ \.php$ {
        include  fastcgi_params;
        # check your /etc/php5/fpm/pool.d/www.conf to see if PHP-FPM is listening on a socket or port
        fastcgi_pass  unix:/run/php/php7.2-fpm.sock; ## listen for socket
        #fastcgi_pass  127.0.0.1:9000; ## listen for port
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        try_files  $uri =404;
    }

    #error_page  404 /404.html;

    location = /requirements.php {
        deny all;
    }
    location ~ \.(ht|svn|git) {
        deny all;
    }
}

【讨论】:

  • 欢迎来到 Stack Overflow!虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 2013-04-21
  • 2017-06-06
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2022-01-17
相关资源
最近更新 更多