Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

nginx server_name wildcard or catch-all

I have an instance of nginx running which serves several websites. The first is a status message on the server's IP address. The second is an admin console on admin.domain.com. These work great. Now I'd like all other domain requests to go to a single index.php - I have loads of domains and subdomains and it's impractical to list them all in an nginx config.

So far I've tried setting server_name to * but that failed as an invalid wildcard. *.* works until I add the other server blocks, then I guess it conflicts with them.

Is there a way to run a catch-all server block in nginx after other sites have been defined?

N.B. I'm not a spammer, these are genuine sites with useful content, they're just powered by the same CMS from a database!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change listen option to this in your catch-all server block. (Add default_server) this will take all your non-defined connections (on the specified port).

listen       80  default_server;

if you want to push everything to index.php if the file or folder does not exist;

try_files                       $uri /$uri /index.php;

Per the docs, It can also be set explicitly which server should be default, with the **default_server** parameter in the listen directive


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...