0

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?

1

1 Answer 1

3

The server block should be inside the http block.

This is mentioned in the NGINX documentation at the link below (See where it says 'context'): http://nginx.org/en/docs/http/ngx_http_core_module.html#server

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .