Forum: NGINX Multiple URL parameters in the same location directive.

C4c5511fb3afa09f6dd0e63119f4e98f?d=identicon&s=25 Quintin Par (Guest)
on 2012-10-28 05:00
(Received via mailing list)
Hi all,

I have some URL patterns that follow the same directive specifications.

E.g. sitemap.xml, robots.txt etc. follow the same location directive
with
the same caching policy, invalidation etc.

        location /robots.txt {

            proxy_pass  http://localhost:82;

            proxy_set_header   Host             $host;

            proxy_set_header   X-Real-IP        $remote_addr;

            proxy_set_header   X-Forwarded-For
$proxy_add_x_forwarded_for;

            proxy_set_header Accept-Encoding "";

            proxy_ignore_headers Cache-Control;

            proxy_ignore_headers Expires;

            proxy_ignore_headers X-Accel-Expires;

            proxy_cache             cache;

            proxy_cache_key         $scheme$host$request_uri;

            proxy_cache_valid       200 302 2m;

            proxy_cache_use_stale   updating;

        }

Now I have written separate directives for all of these URL patterns.

How can I combine them into one line?

Can I do this?

        location /robots.txt, sitemap.xml, ~*.xml {

etc. ?
B0b357b291ac72bc7da81b4d74430fe6?d=identicon&s=25 Barry Abrahamson (Guest)
on 2012-10-28 05:35
(Received via mailing list)
Attachment: signature.asc (880 Bytes)
On 10/27/12 11:59 PM, Quintin Par wrote:

> How can I combine them into one line?

You *could* combine them into one line using a regex, but I would not
recommend it - it will be more complicated to maintain over time and
less readable.  I would recommend putting all of your common
configuration directives into a separate file and including it in each
individual location block.  Something like this:

location = /robots.txt {
  include common-config.conf;
}

location = /sitemap.xml {
  include common-config.conf;
}

...

I would also be sure to read the documentation on location syntax [0] to
understand the differences between exact, prefix, and regex matches so
you are sure to use the correct type for each pattern you are trying to
match.

Hope this is useful.

References:
0. http://nginx.org/en/docs/http/ngx_http_core_module...
C4c5511fb3afa09f6dd0e63119f4e98f?d=identicon&s=25 Quintin Par (Guest)
on 2012-10-28 11:25
(Received via mailing list)
Is there a way I can specify this in the same file itself? I dont want
to
spread this out to multiple files.

Include a section?
- Quintin
34011bc56457235a2caa5ed1d4a29f3c?d=identicon&s=25 Jonathan Matthews (Guest)
on 2012-10-28 15:38
(Received via mailing list)
On 28 October 2012 03:59, Quintin Par <quintinpar@gmail.com> wrote:
> Hi all,
>
> I have some URL patterns that follow the same directive specifications.
>
[snip]
> Now I have written separate directives for all of these URL patterns.
> How can I combine them into one line?
>
> Can I do this?
>         location /robots.txt, sitemap.xml, ~*.xml {

Not quite.

You'll need to use a regular expresion in your location, as detailed
at http://nginx.org/r/location and
http://wiki.nginx.org/HttpCoreModule#location

I find the wiki documentation on this specific topic to be clearer and
more useful.

Jonathan
--
Jonathan Matthews // Oxford, London, UK
http://www.jpluscplusm.com/contact.html
34011bc56457235a2caa5ed1d4a29f3c?d=identicon&s=25 Jonathan Matthews (Guest)
on 2012-10-28 15:41
(Received via mailing list)
On 28 October 2012 10:25, Quintin Par <quintinpar@gmail.com> wrote:
> Is there a way I can specify this in the same file itself? I don’t want to
> spread this out to multiple files.
>
> Include a section?

Yes, you can. Here you go. http://bit.ly/RdVWFo

(A mailing list is not the place to ask *trivially* answerable
questions!)

Jonathan
--
Jonathan Matthews // Oxford, London, UK
http://www.jpluscplusm.com/contact.html
C4c5511fb3afa09f6dd0e63119f4e98f?d=identicon&s=25 Quintin Par (Guest)
on 2012-10-29 05:20
(Received via mailing list)
Thanks Jon. I was desperately looking for a way to include this in  the
same file and in my experience there are a lot of things like this that
are
not documented in the wiki elsewhere but Maxim, Sergey, Antonio etc.
seem
to know.

On Sun, Oct 28, 2012 at 8:10 PM, Jonathan Matthews
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.