Nginx与www和没有www

我有一个nginx的服务器指令:

server { listen 80; server_name mydomain.net; root /home/sites/mydomain.net; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /home/sites/mydomain.net$fastcgi_script_name; fastcgi_index index.php; } } 

这在我访问mydomain.net时效果很好。 但是,如果用户尝试使用www.mydomain.net,那么这里没有任何东西说要应用这些东西

我所做的search显示,有一个完整的其他指令,用server_name www.mydomain.net监听,看起来不是很好。 例如

 server { listen 80; server_name www.mydomain.net; root /home/sites/mydomain.net; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /home/sites/mydomain.net$fastcgi_script_name; fastcgi_index index.php; } } 

基本上加倍configuration代码。 或者以其他方式进行重写。

有没有这样做的较less的方式?

您可以在server_name使用多个名称,甚至可以重复它。

例如,要回答三个主机名,您可以执行如下操作:

 server { server_name example.com www.example.com; server_name api.example.com;