Nginx yii2配置

a14dhokn  于 2022-11-09  发布在  Nginx
关注(0)|答案(3)|浏览(192)

"你好"
我正在尝试为2个Yii项目配置Nginx,frontend用于用户,admin用于只有一个域的管理员(无子域)。我需要将其配置为使mydomain.com引用frontendmydomain.com/admin引用admin。问题是我一次只能配置其中一个,这意味着我可以使用frontend或admin,但不能同时使用两者。

  • 我所尝试的 *
    前端配置
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;
    }
}

返回.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;
    }
}

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

m2xkgtsf

m2xkgtsf1#

我最近使用了类似配置来支持单个域上的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;
        }
}

也可以查看Yii 2的官方文档,在单个域(Apache,Nginx). CLICK HERE上设置yii 2-app-advanced
还有一件事,你需要知道的是,如果你想改变backend/webadmin,那么你也必须在Yii 2应用程序中做一些改变。

ar7v8xwq

ar7v8xwq2#

一个域会将所有的请求引导到一个IP(服务器)。Nginx会使用第一个server块匹配server_name https://nginx.org/en/docs/http/request_processing.html,所以你需要把所有的配置放在一个文件中,并使用location来分隔它们。你可以把location ^~ /admin移到front.conf位置的开头,并使用根目录;或者,您可以创建一个代理配置文件,其中只包含一小部分内容。

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

使用后一种方式,你应该改变前后配置来监听其他端口。另外,SSL证书是为一个域而不是URL提供的。所以你只能在代理配置中使用它。

edqdpe6u

edqdpe6u3#

如果您遵循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

相关问题