> For the complete documentation index, see [llms.txt](https://petercheng7788.gitbook.io/developer-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://petercheng7788.gitbook.io/developer-note/devop/nginx.md).

# Nginx

## Introduction

* Nginx can act as a web server and  reverse proxy

## Example

{% code title="nginx.conf" %}

```nginx
events {}

http {

    # http
    server {
        listen 80;
        server_name ${DOMAIN_NAME};
        # redirect to https
        location / {
            return 301 https://$host$request_uri;
        }
        # host the static content
        location /images/ {
            root /data;
        }
    }
    
   # https
    server {
        listen 443 ssl;
        server_name ${DOMAIN_NAME};
        ssl_certificate /etc/nginx/ssl/certificate.crt;
        ssl_certificate_key /etc/nginx/ssl/private.key;
         # Forward to the application
        location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_set_header   Host $http_host;
            proxy_pass         "http://n8n:5678";
        }
    }
    
    # Load balancing example
    upstream load_balance_server {
        #weigth参数表示权值，权值越高被分配到的几率越大
        server 192.168.1.11:80   weight=5;
        server 192.168.1.12:80   weight=1;
        server 192.168.1.13:80   weight=6;
    }

   
   server {
        #listen to 8080 port
        listen       8080;

        #Define using www.xx.com 
        server_name  www.helloworld.com;

        #对所有请求进行负载均衡请求
        location / {
            root        /root;                 #定义服务器的默认网站根目录位置
            index       index.html index.htm;  #定义首页索引文件的名称
            proxy_pass  http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表

            #以下是一些反向代理的配置(可选择性配置)
            #proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_connect_timeout 90;          #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_send_timeout 90;             #后端服务器数据回传时间(代理发送超时)
            proxy_read_timeout 90;             #连接成功后，后端服务器响应时间(代理接收超时)
            proxy_buffer_size 4k;              #设置代理服务器（nginx）保存用户头信息的缓冲区大小
            proxy_buffers 4 32k;               #proxy_buffers缓冲区，网页平均在32k以下的话，这样设置
            proxy_busy_buffers_size 64k;       #高负荷下缓冲大小（proxy_buffers*2）
            proxy_temp_file_write_size 64k;    #设定缓存文件夹大小，大于这个值，将从upstream服务器传

            client_max_body_size 10m;          #允许客户端请求的最大单文件字节数
            client_body_buffer_size 128k;      #缓冲区代理缓冲用户端请求的最大字节数
        }
    }
    
    
    # default setting for http and https
    server {
        listen 80 default_server ;
        server_name _;
        return 403 "forbidden"; # 403 forbidden
    }

    server {
        listen 443 ssl ;
        server_name ${IP_ADDRESS};
        ssl_certificate /etc/nginx/ssl/certificate.crt;
        ssl_certificate_key /etc/nginx/ssl/private.key;
        return 403 "forbidden"; # 403 forbidden
    }
}
```

{% endcode %}

{% code title="docker.compose.yml" %}

```yaml
version: '3.7'

services:
  nginx: 
    image: nginx
    restart: always
    ports: 
      - "80:80"
      - "443:443"
    volumes: 
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./certificate.crt:/etc/nginx/ssl/certificate.crt
      - ./private.key:/etc/nginx/ssl/private.key
  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: always
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=${POSTGRES_HOST}
      - DB_POSTGRESDB_PORT=5432
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - WEBHOOK_URL=https://${DOMAIN_NAME}/
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
    ports:
      - 5678:5678
    volumes:
      - n8n_data:/home/node/.n8n
      - ./local_files:/files
volumes:
  n8n_data:
    external: true
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://petercheng7788.gitbook.io/developer-note/devop/nginx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
