iwekesi
iwekesi

Reputation: 2366

Testing of Nginx server block

I have installed Nginx 1.12.0 on my laptop.

I have a simple Nginx server block as follows:

server {
    listen         80;
    server_name    myserver.com;

    location /hello {
       return 200 'OK';
    }
}

I want to test the working of this server block. One method I can think of is :

Is there any other method without editing the /etc/hosts file? I mean is it possible using just a curl command (probably using some headers like x-forwarded-server or something) ?

Upvotes: 1

Views: 15141

Answers (2)

Maroun
Maroun

Reputation: 95998

You should add "Host" to the request header to determine which server the request should be routed to. For example, you can do the following:

curl -H 'Host: myserver.com' localhost:80/hello

Upvotes: 3

Hans Z.
Hans Z.

Reputation: 54038

The server_name directive used to identify virtual hosts, it is not used to set the binding. Since the listen 80 directive makes NGINX serve requests on all IP addresses, including 127.0.0.1, you can use:

curl http://127.0.0.1/hello

or

curl http://localhost/hello

without /etc/hosts changes, the latter assuming that 127.0.0.1 localhost is already in there by default.

Upvotes: 1

Related Questions