• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: setting up return directive

December 18, 2023

Nginx for Newbie: Setting up Return Directive

Nginx is a powerful web server and reverse proxy server that is widely used for hosting websites and applications. It offers various features and directives that allow users to customize their server configurations. One such directive is the “return” directive, which is used to control the server’s response to client requests. In this article, we will explore how to set up the return directive in Nginx for beginners.

Understanding the Return Directive

The return directive in Nginx is used to specify the server’s response to a client request. It allows you to return a specific HTTP status code along with an optional response body. The return directive is often used in conjunction with conditional statements to control the flow of requests and responses.

Setting up the Return Directive

To set up the return directive in Nginx, you need to modify your server configuration file. The configuration file is usually located in the “/etc/nginx” directory and is named “nginx.conf” or “default.conf”. Here are the steps to follow:

Step 1: Open the Configuration File

Use a text editor to open the Nginx configuration file. For example, you can use the following command:

sudo nano /etc/nginx/nginx.conf

Step 2: Locate the Server Block

Inside the configuration file, locate the server block that corresponds to the website or application you want to configure. The server block typically starts with the “server” keyword and includes the server’s listen address and port.

Step 3: Add the Return Directive

Within the server block, you can add the return directive to specify the server’s response. The basic syntax of the return directive is as follows:

return <status_code> <response_body>;

Replace “<status_code>” with the desired HTTP status code, such as 200, 404, or 500. You can also include an optional response body by replacing “<response_body>” with the desired content.

Step 4: Save and Exit

After adding the return directive, save the configuration file and exit the text editor. For example, in the nano editor, you can press “Ctrl + X”, then “Y” to save the changes.

Examples

Let’s look at a few examples of how to use the return directive in Nginx:

Example 1: Returning a 404 Error

server {
    listen 80;
    server_name example.com;

    location / {
        return 404;
    }
}

In this example, any request to the server’s root (“/”) will result in a 404 Not Found error.

Example 2: Redirecting to Another URL

server {
    listen 80;
    server_name example.com;

    location /old-url {
        return 301 http://example.com/new-url;
    }
}

In this example, any request to “/old-url” will be redirected to “http://example.com/new-url” with a 301 Moved Permanently status code.

Summary

The return directive in Nginx is a powerful tool for controlling the server’s response to client requests. By using this directive, you can specify the HTTP status code and response body to be returned. Whether you want to return an error, redirect to another URL, or customize the server’s response, the return directive provides the flexibility to do so.

If you are interested in learning more about VPS hosting and how it can benefit your website or application, consider exploring Server.HK. With their top-notch VPS solutions, you can enjoy reliable and high-performance hosting for your online endeavors.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot