Best way to do what you want is to add another server block:

server {
        #implemented by default, change if you need different ip or port
        #listen *:80 | *:8000;
        server_name test.com;
        return 301 $scheme://www.test.com$request_uri;
}

And edit your main server block server_name variable as following:

server_name  www.test.com;

Important: New server block is the right way to do this, if is evil. You must use locations and servers instead of if if it's possible. Rewrite is sometimes evil too, so replaced it with return.

How to Create NGINX Rewrite Rules | NGINX

https://www.nginx.com/blog/creating-nginx-rewrite-rules/

Oct 7, 2015 ... Learn how to create NGINX rewrite rules to control the flow of request processing or redirect clients to the correct location for resources they ...

rewrite module directives

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

... directives are put inside the “ /download/ ” location, the last flag should be replaced by break , or otherwise nginx will make 10 cycles and return the 500 error:.

Nginx中的Rewrite的重定向配置与实践- 龙恩0707 - 博客园

https://www.cnblogs.com/tugenhua0707/p/10798762.html

2019年5月1日 ... 简介:Rewrite是Nginx服务器提供的一个重要的功能,它可以实现URL重定向功能 。 回到顶部. 一:理解地址重写与地址转发的含义。 地址重写与 ...

Rewrite - NGINX Ingress Controller

https://kubernetes.github.io/ingress-nginx/examples/rewrite/

nginx.ingress.kubernetes.io/rewrite-target, Target URI where the traffic must be ... nginx.ingress.kubernetes.io/ssl-redirect, Indicates if the location section is ...

Nginx基础知识】详解nginx配置url重定向-反向代理_Marion的博客 ...

https://blog.csdn.net/luomao2012/article/details/81167945

2018年7月23日 ... $uri, 请求的URI,可能和最初的值有不同,比如经过重定向之类的。 rewrite 指令: 可以使用在server, location, if 区域;. 语法:rewrite regex ...

nginx 301重定向设置- 简书

https://www.jianshu.com/p/aa0846c9c650

2017年11月7日 ...nginx中配置,使得访问不带www的网址自动重定向到带www的域名。 http协议 的重定向nginx官方文档中有如下示例代码: 对于return字段, ...

URL redirection - Wikipedia

https://en.wikipedia.org/wiki/URL_redirection

Nginx has an integrated http rewrite module, which can be used to perform advanced URL processing and even web-page generation (with the return directive).

nginx实现内容重定向的两种方式:rewrite和反向代理- SegmentFault ...

https://segmentfault.com/a/1190000014475115

2018年4月18日 ... 按照常规理解,实现重定向就是要用rewrite来实现,例如demo:当访问test.cc/uri, 如果uri资源在项目中不存在,就会重定向[链接] 网页。

Nginx Redirect Cheat Sheet | Liquid Web

https://www.liquidweb.com/kb/redirecting-urls-using-nginx/

Mar 29, 2019 ... Check out our Nginx tutorials for redirecting a site to HTTPS, www, non-www, another page or a completely different URL.

How to Use Nginx to Redirect to HTTPS, www/non-www and More!

https://www.hostinger.com/tutorials/nginx-redirect/

Jul 13, 2020 ... In Nginx, most redirects can be achieved with the help of inbuilt rewrite feature. This is the default feature that's available on a clean installation of ...

Redirect HTTP to HTTPS in Nginx | Servers for Hackers

https://serversforhackers.com/c/redirect-http-to-https-nginx

May 18, 2018 ... We see how to use Nginx to redirect any non-SSL connection to HTTP. Beginners.

How To Create Temporary and Permanent Redirects with Nginx ...

https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-nginx

Dec 19, 2016 ... In Nginx, you can accomplish most redirects with the built-in rewrite directive. This directive is available by default on a fresh Nginx installation ...

Annotations - NGINX Ingress Controller

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/

Without a rewrite any request will return 404. Set the annotation nginx.ingress. kubernetes.io/rewrite-target to the path expected by the ...

How to redirect a URL in Nginx - Stack Overflow

https://stackoverflow.com/questions/10294481/how-to-redirect-a-url-in-nginx

Best way to do what you want is to add another server block: server { # implemented by default, change if you need different ip or port #listen ...

How to Redirect HTTP to HTTPS in Nginx | PhoenixNAP KB

https://phoenixnap.com/kb/redirect-http-to-https-nginx

Oct 15, 2019 ... In this tutorial learn how to Redirect HTTP to HTTPS in Nginx. Get started Redirecting all HTTP traffic, specific sites, and domains now!

Redirect HTTP to HTTPS in Nginx | Linuxize

https://linuxize.com/post/redirect-http-to-https-in-nginx/

Sep 26, 2019 ... In this guide, we will explain how to redirect the HTTP traffic to HTTPS in Nginx. Nginx pronounced “engine x” is a free, open-source, ...