Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use multiple locations in vhost #189

Closed
jacek-berlin opened this issue Nov 23, 2013 · 14 comments
Closed

Allow to use multiple locations in vhost #189

jacek-berlin opened this issue Nov 23, 2013 · 14 comments

Comments

@jacek-berlin
Copy link

Hello,
Please correct me if I am wrong, now there is no possibility to declare multiple locations in one vhost ?

Example output configuration:

http://doc.owncloud.org/server/5.0/admin_manual/installation/installation_others.html

@deric
Copy link
Contributor

deric commented Nov 23, 2013

Sure it is possible. Try something like this:

nginx::resource::location { '/':
  ensure   => present,
  try_files => ['$uri' '$uri/' 'index.php'],
  location_cfg_prepend => {
    'rewrite ^/.well-known/host-meta'        => '/public.php?service=host-meta last',
    'rewrite ^/.well-known/host-meta.json'   => '/public.php?service=host-meta-json last;'
  }
}

nginx::resource::location { '~ ^(.+?\.php)(/.*)?$':
  ensure   => present,
  try_files => ['$1 = 404'],
  fastcgi   => '127.0.0.1:9000'
}

I haven't test the code, hope it will compile :)

@jacek-berlin
Copy link
Author

thanks, I've tried it like this, essentialy it works but I can't get it inside server { ... }

@hackzilla
Copy link

Try adding a vhost line.

nginx::resource::location { '~ ^(.+?\.php)(/.*)?$':
  ensure   => present,
  vhost  => '...',
  try_files => ['$1 = 404'],
  fastcgi   => '127.0.0.1:9000'
}

@ghost
Copy link

ghost commented Dec 4, 2013

I'm running into the same issue, the location is being created outside of the server block.

@3flex
Copy link
Contributor

3flex commented Dec 4, 2013

Make sure vhost in nginx::resource::location matches the name of the vhost you created:

nginx::resource::vhost { 'vhost_name':
  ensure => present
  #...
}

nginx::resource::location { 'location_name':
  ensure => present,
  vhost  => 'vhost_name'
  #...
}

Also make sure you don't set the priority parameter in nginx::resource::location unless you know what you're doing.

If that doesn't work can you post the full manifest you're using?

@sherzberg
Copy link

I just ran into this too. It works fine if you have what @3flex says if you use $name as the vhost. If you don't it doesn't seem to work. Here is my example that will not put the location block inside server{...}

nginx::resource::vhost { 'beta-redirect':
  ensure              => present,
  server_name         => ['beta.examle.com'],
  proxy               => 'http://example-beta',
  location_cfg_append => {
    'return 301' => 'https://beta.example.com$request_uri',
  }
}

nginx::resource::vhost { 'beta.example.com':
  ensure                                    => present,
  listen_port                               => '443',
  proxy                                     => 'http://example-beta',
  ssl                                       => true,
  ssl_cert                                  => '/etc/example/ssl/beta.example.com.crt',
  ssl_key                                   => '/etc/example/ssl/beta.example.com.key',
  location_cfg_append                       => {
    'proxy_set_header X-Forwarded-Protocol' => 'https',
    'proxy_set_header Host'                 => '$http_host',
    'proxy_set_header X-Real-IP'            => '$remote_addr',
    'proxy_set_header X-Scheme'             => '$scheme',
  }
}

nginx::resource::location {'beta.example.com-static':
  vhost                        => 'beta.example.com',
  ensure                       => 'absent',
  location                     => '/static',
  proxy                        => 'http://example-beta',
  location_cfg_prepend         => {
    'expires'                  => 'max',
    'add_header Cache-Control' => 'public',
  }
}

Not sure if the rest of my config is the best way to do it so I am welcome to input on that as well :)

@emning
Copy link

emning commented Dec 21, 2013

I just ran into the same problem when using ssl and spent way too long figuring it out, assuming it was a bug. (In a way I still think it is.)

Locations that belong in an SSL vhost need the ssl parameter set to true. If you don't have a non-SSL vhost at all then locations missing this will end up outside any server block. If you have vhosts of both kinds then ssl_only determines whether your location will end up in both, or just in the SSL one.

In the case of @sherzberg's config the solution would be to add this to the location:

ssl      => true,
ssl_only => true,

@sherzberg
Copy link

@emning thanks for the tip. Just implemented this and it works great!

@3flex
Copy link
Contributor

3flex commented Jan 10, 2014

@emning @sherzberg I added a section the README regarding SSL and vhost/location relationships, could you let me know if this clarifies things at all? Thanks!

https://github.com/jfryman/puppet-nginx#ssl-configuration

@emning
Copy link

emning commented Jan 11, 2014

Looks very good to me, @3flex. I would say this issue can be closed. Well done!

@3flex
Copy link
Contributor

3flex commented Jan 14, 2014

@jacek-berlin would you consider closing this if there's nothing outstanding?

@sherzberg
Copy link

@3flex looks like exactly what I needed! Thanks for taking the time to add it to the docs!

@devopsdevon
Copy link

I would say this behavior is a bug.
If you have an SSL only vhost and a non-SSL location (or a non-SSL-only location), the module generates an invalid Nginx configuration that prevents the service from starting.

Having the module throw an error instead would be great.

@3flex
Copy link
Contributor

3flex commented Apr 10, 2015

I'm closing this, as it looks like the original issue was addressed.

@devopsdevon would you open a new issue for the behaviour you described? Thanks!

@3flex 3flex closed this as completed Apr 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants