【问题标题】:Nginx yii2 configurationNginx yii2 配置
【发布时间】:2019-12-31 20:45:50
【问题描述】:

你好!

我正在尝试为 2 个 yii 项目配置 Nginx,frontend 用于用户,admin 用于只有一个域(无子域)的管理员。我需要以 mydomain.com 应该引用 frontendmydomain.com/adminadmin。问题是我一次只能配置其中一个,这意味着我可以使用前端或管理员而不是两者。

我的尝试

front.conf

server {
        listen 80;
        server_name api.maim.experiments.uz;
        return 301 https://$server_name$request_uri;
}


server {
    charset utf-8;
    client_max_body_size 128M;

    listen 443 ssl;

    ssl_certificate_key privkey.pem;
    ssl_certificate     fullchain.pem;

    ssl_protocols TLSv1.2;

    set $host_path "/home/itschool/inha_dev/frontend";   

    server_name  api.maim.experiments.uz;
    root        $host_path/web;

    set $yii_bootstrap "index.php";

    access_log  /var/log/nginx/itschool-access.log;
    error_log   /var/log/nginx/itschool-error.log;

    location / {
        index index.html $yii_bootstrap;
        try_files $uri $uri/ /index.php;
    }

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

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

    location ~ \.php$ {

        set $fsn /index.php;
           if (-f $document_root$fastcgi_script_name){
               set $fsn $fastcgi_script_name;
        }

        fastcgi_pass 127.0.0.1:9002;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fsn;
    }

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

    location ~* /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

back.conf

server {
        listen 80;
        server_name api.maim.experiments.uz;
        return 301 https://$server_name$request_uri;
}


server {
    charset utf-8;
    client_max_body_size 128M;

    listen 443 ssl;

    ssl_certificate_key privkey.pem;
    ssl_certificate     fullchain.pem;

    ssl_protocols TLSv1.2;

    set $host_path "/home/itschool/inha_dev/backend";   

    server_name  api.maim.experiments.uz;
    root        $host_path/web;

    set $yii_bootstrap "index.php";

    access_log  /var/log/nginx/itschool-access.log;
    error_log   /var/log/nginx/itschool-error.log;


    location ^~ /admin {
        alias /home/itschool/inha_dev/backend/web;

        if (!-e $request_filename) { rewrite ^ /admin/index.php last; }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_pass   127.0.0.1:9002;
        }
    }

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

    location ~* /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

我发现了一些问题的答案,但它们对我不起作用,请帮助。

【问题讨论】:

标签: nginx yii centos


【解决方案1】:

我最近使用类似的配置来支持单个域上的 Web 应用程序/移动应用程序和管理面板

我希望这可以帮助你。下面是配置

server {
        listen 80;

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

        #here we go
        #if backend not found in url then set root url
        if ($uri !~ "^(.*)/(backend)(.*)") {
            set $root /var/www/html/application/frontend/web;
        }

        # when request is coming from mobile then display mobile site
        # you don't need this one, I just written in order to explain the mobile application navigation.
        if ($http_user_agent ~* "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos") {
            set $root /var/www/html/application/mobile/web;
        }

        root $root;

        index index.php index.html index.htm index.nginx-debian.html;
        server_name your_domain;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location / {
            index  index.html index.php;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }

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

还可以查看 Yii2 的官方文档以在单个域(Apache、Nginx)上设置 yii2-app-advancedCLICK HERE

您需要知道的另外一件事是,如果您想将 backend/web 更改为 admin,那么您还必须在 Yii2 应用程序中进行一些更改。

【讨论】:

    【解决方案2】:

    一个域会将所有请求引导到一个 IP(服务器)。 Nginx 将使用与 server_name https://nginx.org/en/docs/http/request_processing.html 匹配的第一个 server 块,因此您需要将所有配置放在一个文件中并使用 location 将它们分开。 您可以将location ^~ /admin 移动到front.conf 位置的开头并与根一起玩; 或者你可以创建一个只包含一点的代理配置文件。 类似的东西

    location /admin {
        proxy_pass http://localhost:8001;
    }
    location / {
        proxy_pass http://localhost:8002;
    }
    

    使用后者,您应该更改前后配置以侦听其他端口。此外,为域而不是 URL 提供了 SSL 证书。所以你只能在代理配置中使用它。

    【讨论】:

      【解决方案3】:

      如果您遵循Yii2 Single Domain Apache and Nginx 选项 1 中的一些关键说明,您应该能够完成您想要的。

      根据引用的链接,选项 1:

      假设 Linux 操作系统

      cd /path/to/project/frontend/web
      ln -s ../../backend/web backend
      

      并设置你的 nginx 文件

      server {
          charset utf-8;
          client_max_body_size 128M;
      
          listen 80; ## listen for ipv4
          #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
      
          server_name api.maim.experiments.uz;
          root        /home/itschool/inha_dev/frontend/web;
          index       index.php;
      
          access_log  /var/log/nginx/itschool-access.log;
          error_log   /var/log/nginx/itschool-error.log;
      
          location / {
              # Redirect everything that isn't a real file to index.php
              try_files $uri $uri/ /index.php$is_args$args;
          }
      
          # uncomment to avoid processing of calls to non-existing static files by Yii
          #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
          #    try_files $uri =404;
          #}
          #error_page 404 /404.html;
      
          # deny accessing php files for the /assets directory
          location ~ ^/assets/.*\.php$ {
              deny all;
          }
      
          location ~ \.php$ {
              include fastcgi_params;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_pass 127.0.0.1:9000;
              #fastcgi_pass unix:/var/run/php5-fpm.sock;
              try_files $uri =404;
          }
      
          location ~* /\. {
              deny all;
          }
      }
      

      不:如果以上方法不起作用,请参见下面的选项 2 链接:

      Yii2 Single Domain Apache and Nginx

      【讨论】:

        猜你喜欢
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 2017-07-25
        • 2012-12-31
        • 2015-08-28
        相关资源
        最近更新 更多