I’m using nginx (1.4.6-1ubuntu3.4) on Ubuntu 14.04. When someone visits a page, e.g. http://mymaindomain.com/page1
, I want to rewrite it to include the www
subdomain. So if someone visited the previous URL, the rewritten URL would be http://www.mymaindomain.com/page1
. So I added a “server” directive:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
…
}
server {
server_name mymaindomain.com;
rewrite ^ http://www.mymaindomain.com$request_uri permanent;
}
to the end of my /etc/nginx/nginx.conf
file. But after restarting my server, I get the error:
2016/11/04 22:12:33 [emerg] 1063#0: "server" directive is not allowed here in /etc/nginx/nginx.conf:75
What’s the right way to set up my nginx server to properly rewrite my URL?