仅在nginx中重写域的根

我已经运行了YOURLS服务器来为我的公司提供简短的URL,以便我们可以向客户提供简短的URL。

我使用nginx,并为域的根,如果他们不使用适当的短url我想redirect到我们的网站。 因此,example.com/218aredirect到任何简短的url指向,但example.com去我们的网站@ domain.com。

现在,我正在使用一个index.html页面,只是一个元刷新redirect,但我想我应该能够在nginxconfiguration中做到这一点,它可能会更好。

有人可以帮助我,如果可能的话,在服务器级而不是使用元刷新发生?

我已经尝试了一些我在这里find的例子,但是没有一个能够工作。

这是我的configuration文件供参考。

server { server_name www.domain.com; return 301 $scheme://domain.com$request_uri; } server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm index.php; # Make site accessible from http://localhost/ server_name http://domain.com; location / { #YOURLS try_files $uri $uri/ /yourls-loader.php; } location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass php5-fpm-sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } } 

首先, server_name指令在你的主server块中是无效的。 server_name只包含域名,而不包含URL的任何其他部分。

要回答您的实际问题,请添加以下configuration:

 location = / { rewrite ^ http://domain.com permanent; } 

这使所有匹配的确切/位置的URIredirect到http://domain.com