深度阅读

How to set up a reverse proxy with nginx?

作者
作者
2023年08月22日
更新时间
11.08 分钟
阅读时间
0
阅读量

To set up a reverse proxy with nginx, you can use the proxy_pass directive in your nginx configuration file. Here is a basic example to proxy requests from example.com to localhost:3000:

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

In this example, listen 80; specifies that nginx should listen for HTTP requests on port 80. server_name specifies the domain name for which you want to proxy requests to localhost:3000. location / specifies the path to match for incoming requests and proxy_pass specifies the backend server to which requests should be forwarded.

proxy_set_header Host $host; and proxy_set_header X-Real-IP $remote_addr; sets the Host and X-Real-IP headers in the forwarded requests to ensure that the backend server receives the requests with the correct headers and IP address.

Note that this is a basic example to help you get started. You may need to modify the configuration to suit your specific needs.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。