nginx服务器配置网站模板

server {
    listen 127.0.0.1:80;
    server_name www.test.com test.com;
    if ( $host = 'test.com' ) {
            rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }

        set $ROOT_PATH /home/sites/test/public;

        location = / {
                root $ROOT_PATH;
                index index.php;
                try_files $uri $uri/ /index.php?$query_string;
        }


        root /home/sites/test/public;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
        index index.php index.html index.htm;
        charset utf-8;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; }
    error_page 404 /index.php;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $ROOT_PATH$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }
    
    access_log /home/log/nginx/access/www.test.com.access.log;
    error_log /home/log/nginx/error/www.test.com.error.log;
}

#####################################################

若该域名有老代码,且打算继续使用

 server {
        listen       127.0.0.1:80;
        server_name  www.test.com test.com;
        if ( $host = 'test.com' ) {
            rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }

                set $ROOT_PATH /home/sites/test/public/;

         location = / {
                root $ROOT_PATH;
                index index.php;
                try_files $uri $uri/ /index.php?$query_string;
        }
#####引用老代码配置,用正则表达式判断路由参数,不符合则跳入老代码
        location ~ ^/(?!school/|news/|search/|index\.php|css|js|images|fonts|baidu_verify|sitemap|robots).*$ {
                proxy_pass http://proxypass.test.com;
        }


        root /home/sites/test/public/;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
        index index.php index.html index.htm;
        charset utf-8;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt { access_log off; log_not_found off; }
        error_page 404 /index.php;
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $ROOT_PATH$fastcgi_script_name;
                include fastcgi_params;
        }
        location ~ /\.(?!well-known).* {
                deny all;
        }

        access_log /home/log/nginx/access/www.test.com.access.log;
        error_log /home/log/nginx/error/www.test.com.error.log;
}

猜你喜欢

转载自www.cnblogs.com/lst-315/p/10078283.html