Answer a question

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.

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>';
    }
}
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐