【问题标题】:Nginx configuration for friendly urls友好 url 的 Nginx 配置
【发布时间】:2014-05-17 08:24:37
【问题描述】:

你好,

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

这是我目前的配置:

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

}

不带 www 的 url 的第一个块

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/”的目录索引被禁止”。

当我同时评论根目录和索引时,我得到 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。

【问题讨论】:

    标签: nginx configuration friendly-url


    【解决方案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

    【讨论】:

    • 感谢您的回答。我会试着告诉你会发生什么。
    • 我还在同一点。现在我再次得到 403 禁止。在我要求添加友好网址之前,您建议的配置似乎与我的配置相似。所以如果我是对的,这个配置会将所有接受 js css 和图像的东西传递给 apache。那就是说我不想使用 apache 重写引擎,我要求使用 try_files 指令来做,但仍然没有发生任何事情。感谢您的回应。
    【解决方案2】:

    甚至认为@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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-26
      • 2017-05-30
      • 1970-01-01
      • 2017-02-07
      • 2012-10-08
      • 2012-04-08
      • 2017-03-21
      • 1970-01-01
      相关资源
      最近更新 更多