通过子URL在nginx服务器上提供服务的配置

时间:2012-04-25 16:46:00

标签: nginx vhosts

我们有两个nginx服务器。第一台服务器通过www.example.com/partner接收请求。他将整个请求发送到配置了php + fastCgi的第二台服务器。 Nginx从第二台服务器访问日志:

  

“GET / partner / HTTP / 1.0”200 2845

在第二台服务器上,我有一个看起来像这样的虚拟主机:

server {
       listen my.ip:80;
       server_name www.example.com;
       root /var/www/example;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://www.example.com$request_uri permanent;
       }

       index index.php index.html;


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


        location @nocache {
                try_files $uri $uri/ /index.php?$args;
       }
       location = /partner/robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }

       location ^~ /partner/typo3temp/tx_ncstaticfilecache {
                expires 43200;
                charset utf-8;
       }

       location = /partner/clear.gif {
                empty_gif;
                expires max;
       }
       location ^~ /partner/typo3/gfx {
                expires max;
       }
       location ^~ /partner/typo3temp/compressor {
                expires max;
       }   

    location /partner {    


                if ($query_string ~ ".+") {
                        return 405;
                }
                # pass requests from logged-in users to PHP
                if ($http_cookie = 'nc_staticfilecache|be_typo_user' ) {
                        return 405;
                } # pass POST requests to PHP
                if ($request_method !~ ^(GET|HEAD)$ ) {
                        return 405;
                }
                if ($http_pragma = 'no-cache') {
                        return 405;
                }
                if ($http_cache_control = 'no-cache') {
                        return 405;
                }
                error_page 405 = @nocache;

                # serve requested content from the cache if available, otherwise pass the request to PHP
                try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;


       location ~* \.(sql|htaccess|htpasswd|tpl|html5|xhtml) {
                deny all;
       }

       location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
                expires max;
                log_not_found off;
       }

       location ~* \.(cur|ico|gif|png|jpe?g|css|js|swf|woff)((\?\d\d\d\d\d\d\d\d\d\d)|(\?s=\d\d\d\d\d\d\d\d\d\d))$ {
                expires max;
                log_not_found off;
       }
       location ~* \.(cur|ico|gif|png|jpe?g|css|js|swf|woff)(\?v\d\d?\.\d\d?\.\d\d?)$ {
                expires max;
                log_not_found off;
       }
       location ~* ^(/typo3/sysext|/typo3conf/ext).*\.(cur|ico|gif|png|jpe?g|css|js|swf|woff) {
                expires max;
                log_not_found off;
       }



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

       location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_index index.php;
       }
    }
}

我得到的问题和403错误。任何想法都错了吗?

1 个答案:

答案 0 :(得分:1)

最有可能因为php处理位置块嵌套在另一个位置块下而导致php文件被下载(这假设php已经正确设置)。

总的来说,你的配置看起来过于复杂,我确信它所做的任何任务都可以用更直接的配置来完成。

我做了一个基本的清理/整理,但没有完整的图片,有些位可能会关闭。应该给你一些想法。

重要的是,关于PHP将其从嵌套位置拉出来。

此外,由于其他原因,可以合法地使用状态代码418而不是405进行重定向。

将example.com的单独服务器块用于www.example.com重定向,以获得更好的性能。

server {
    listen my.ip:80;
    server_name example.com;
    rewrite ^ http://www.example.com$request_uri? permanent;
}

server {
    listen my.ip:80;
    server_name www.example.com;
    root /var/www/example;
    index index.php index.html;
    error_page 418 = @nocache;

    if ($query_string ~ ".+") {
        return 418;
    }
    # pass requests from logged-in users to PHP
    if ($http_cookie = 'nc_staticfilecache|be_typo_user' ) {
        return 418;
    } 
    # pass POST requests to PHP
    if ($request_method !~ ^(GET|HEAD)$ ) {
        return 418;
    }

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

    location ^~ /partner/typo3temp/tx_ncstaticfilecache {
        expires 43200;
        charset utf-8;
    }

    location = /partner/clear.gif {
        empty_gif;
        expires max;
    }

    location ^~ /partner/typo3/gfx {
        expires max;
    }
    location ^~ /partner/typo3temp/compressor {
        expires max;
    }

    location /partner {
        # serve requested content from the cache if available, 
        # otherwise pass the request to PHP
        try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;
    }

    location ~* \.(sql|tpl|html5|xhtml) {
        deny all;
    }

    location ~*  \.(jpg|jpeg|png|gif|css|js|ico)(.*)$ {
        expires max;
        log_not_found off;
    }

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

    location ~ \.php$ {
        return 418;
    }

    location @nocache {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;            
    }
}
相关问题