Nginx的yii2部署配置

一步一个坑,这样的配置才是好的~~

#frontend配置
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 8080; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.frontend.com;
    root        /var/www/yii-test/backend/web;
    index       index.php;


    access_log  /var/www/yii-test/access.frontend.log main;
    error_log   /var/www/yii-test/error.frontend.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    error_page 404 /404.html;

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

#backend配置
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 8000; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.backend.com;
    root        /var/www/yii-test/backend/web;
    index       index.php;


    access_log  /var/www/yii-test/access.backend.log main;
    error_log   /var/www/yii-test/error.backend.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    error_page 404 /404.html;

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}
server {
        listen 80;
        server_name  b.tdacar.cn;
        root /var/www/hanyi/backend/web;
        index index.php;

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

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /var/www/hanyi/backend/web;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

附网上

server {
    listen 80;
    server_name yiilib.com *.yiilib.com;
    access_log  /var/log/nginx/yiilib.com.access.log  main;
    
    root path_to_Yiilib.com/frontend/web;

    location / {
        if (!-e $request_filename){
            rewrite ^/(.*) /index.php last;
        }
        
        index index.php;
    }
    
    location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
        root /var/www/html/yiilib.com/frontend/web;
    }
    
    location ~\.php$  {
        add_header Cache-Control  "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        fastcgi_split_path_info ^(.+\.php)(/._)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        include fastcgi_params;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = ~/robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        //rules go here

        '<controller>' => '<controller>', // default to site/index
        '<controller>/<action>' => '<controller>/<action>', // default to site/index
    ]
]



导航