Nginx to server secure and not secure traffic

We have secure and no secure domains for our website e.g.
secure.xyz.com
and xyz.com

I used following like to make single server handle both port 80 and 443
traffic.

http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

server {
listen 80;
listen 443 ssl;
server_name secure.xyz.com xyz.com;

ssl_certificate secure.xyz.com.crt;
ssl_certificate_key secure.xyz.com.key;

}

Every thing works fine except that $_SERVER variables ‘SERVER_NAME’ is
set
to ‘secure.xyz.com’ .

My question is :

  1. Does Nginx always picks the first server from the config …
    irrespective
    of what client has requested and passes to proxy (php-fpm)?

  2. We have a lot of rules, so if we create two separate server (as per
    following) do I need to copy the rules in both places? It there any
    maintainable way, like ‘include /common_rules.conf’?

server {
listen 443;
server_name secure.xyz.com;
ssl on;
ssl_certificate secure.xyz.com.crt;

include common_rules.conf; ===>???
}

server {
listen 80;
server_name xyz.com;

include common_rules.conf; ===>???
}

Any help is highly appreciated.

Posted at Nginx Forum:

Hi.
hsrmmr Wrote:

My question is :

  1. Does Nginx always picks the first server from the config …
    irrespective of what client has requested and passes to proxy
    (php-fpm)?

First is actually the server name, and others - is just aliases.
If you want to send requested hostname to backend - use $hostname
instead.

  1. We have a lot of rules, so if we create two separate server (as per
    following) do I need to copy the rules in both places? It there any
    maintainable way, like ‘include /common_rules.conf’?

Yes you can.
Just “include conf/share_config.conf;”

Posted at Nginx Forum:

Yes you can.
Just “include conf/share_config.conf;”

==> I created conf files with locations (which are common), however
after
including Nginx gives error saying " ‘location’ directive is not allowed
here ".
Is there something I am missing?

Posted at Nginx Forum:

Include common_rules.conf works properly now. The problem was that the
.conf
file was also getting included in ‘http’ directive, so it was
complaining
about location directive being invalid.

Thanks !

Posted at Nginx Forum: