友情网址的Nginx配置

时间:2014-05-17 08:24:37

标签: nginx configuration friendly-url

嗯你好,

我正在尝试使用nginx创建友好的网址。

到目前为止,这是我的配置:

server {
    listen 80;
    server_name mysite.gr;
    rewrite ^(.*) http://www.mysite.gr$1 permanent;

}

没有www的网址的第一个区块

server {
    listen 80;
    server_name www.mysite.gr;
    root /var/www/mysite.gr;
    index index.asp;

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

    client_max_body_size 10M;

    location / {
            try_files $uri $uri/ /index.asp?$query_string;
    }

    proxy_set_header        Host            $host;

}

这是主要配置。 当启用root和index指令时,浏览器会下载文件。

当我评论索引指令时,我得到403 Forbidden和nginx错误日志sais“目录索引”/var/www/mysite.gr/“被禁止”。

当我评论root和index时,我得到500错误和错误日志sais“1重写或内部重定向循环,同时内部重定向到”/index.asp“

我想做的是 www.mysite.gr/pertners指向www.mysite.gr/index.asp?q=pertners www.mysite.gr/pertners/the_partner指向www.mysite.gr/index.asp?q=pertners&p=the_partner

有什么建议吗?

顺便说一句* .asp我使用Apache :: ASP和mod_perl。

2 个答案:

答案 0 :(得分:1)

在这种情况下,您需要使用Nginx作为反向代理。这是从代理服务器提供来自Nginx和动态内容(ASP)的静态内容。示例配置如下所示。

server {
    listen 80;
    server_name www.mysite.gr;
    root /var/www/mysite.gr;
    index index.html;

    location / {
        try_files $uri $uri/ @proxy;
    }

    location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
        expires     30d;
    }

    location ~* \.asp$ {
        proxy_pass http://127.0.0.1:8000; # Host and port of where your ASP is running
        include /etc/nginx/proxy_params; # Location and availability depend on your Nginx setup
    }

    location @proxy {
        proxy_pass http://127.0.0.1:8000;
        include /etc/nginx/proxy_params;
    }
}

http://www.google.com/search?q=nginx+reverse+proxy+apache

答案 1 :(得分:0)

甚至认为@ NexWarner的回答让我走到了尽头,他/她的联系让我感到难以忘怀。所以,我稍微改变了他的搜索,经过几次尝试,我想出了以下配置。

server {
    listen 80;
    server_name www.mysite.gr;
    root /var/www/mysite.gr;
    index index.asp;

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

    client_max_body_size 10M;

    #Case we call www.mysite.gr
    #no file so nginx serves controller.asp of corse with empty uri.
    #we do not check for folder($uri/) cause this is the root folder so index.asp will be loaded.
    location = / {
            try_files $uri /controller.asp?uri=$uri;
    }

    #case we call www.mysite.gr/foo or www.mysite.gr/foo/bar
    #$uri is no file neither folder so nginx serves controller.asp with uri=/foo/bar
    location / {
            try_files $uri $uri/ /controller.asp?uri=$uri;
    }

    #ask nginx to serve static file because without this if we read the uri in controller.asp we get .js and .css files too.
    location ~* \.(js|css|jpg|jpeg|gif|png|svg|svgz|ico|pdf|html|htm)$ {
            expires      30d;
   }

    #Finaly i think that this location directive did the magic for me. 
    #we ask nginx to pass to the Apache server everything that is an asp script.
    location ~* \.asp$ {
            proxy_pass http://127.0.0.1:8080;
            proxy_redirect off;
    }

    #Apache will use this Host to determine which name-based VirtualHost will handle the request
    proxy_set_header        Host            $host;
    #Normaly Apache will log the incoming request as coming from nginx.

}

之后我们只是在controller.asp中读取uri并包含我们想要的任何其他文件:

my $Params = $Request->Params();
$Response->Include('foo.asp') if $Params->{uri} eq 'foo';
$Response->Include('foo.asp?q=bar') if $Params->{uri} eq 'foo/bar'; #or whatever equal you like.

所以,我认为@ NexWarner的回答是我的解决方案的开始。 谢谢@NexWarner。