Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX: How to return inline html?

Tags:

nginx

I want to achieve something like this:

server {
    listen 80;
    location / {
        return 200 <html><body>Hello World</body></html>
    }
}

i.e., any request should return the inline html. Is this possible with NGINX?

EDIT:

I tried this:

server {
    listen 80;
    location / {
        return 200 '<html><body>Hello World</body></html>'
    }
}

But testing in browser did not render the html, instead the browser tried to download a file containing the html which is not the behavior I wanted.

like image 448
Ray Zhang Avatar asked Sep 11 '19 17:09

Ray Zhang


1 Answers

Use the return directive to return HTML code. Remember to set proper content type, otherwise the browser will asume raw text and won’t render the code:

server {
    listen 80;

    location / {
        add_header Content-Type text/html;

        return 200 '<html><body>Hello World</body></html>';
    }
}
like image 193
emix Avatar answered Oct 09 '22 16:10

emix