{"id":13459113,"url":"https://github.com/dunwu/nginx-tutorial","last_synced_at":"2025-10-07T22:08:38.564Z","repository":{"id":37752333,"uuid":"139662142","full_name":"dunwu/nginx-tutorial","owner":"dunwu","description":"这是一个 Nginx 极简教程，目的在于帮助新手快速入门 Nginx。","archived":false,"fork":false,"pushed_at":"2024-01-27T15:12:46.000Z","size":3769,"stargazers_count":5125,"open_issues_count":6,"forks_count":721,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-04-12T01:55:03.840Z","etag":null,"topics":["load-balancer","nginx","proxy","tutorial"],"latest_commit_sha":null,"homepage":"https://dunwu.github.io/nginx-tutorial/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-sa-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dunwu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-04T03:07:14.000Z","updated_at":"2025-04-12T01:37:41.000Z","dependencies_parsed_at":"2024-01-13T17:58:33.425Z","dependency_job_id":"4cf01965-d339-4889-9cec-5fe339239bf2","html_url":"https://github.com/dunwu/nginx-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dunwu%2Fnginx-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dunwu%2Fnginx-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dunwu%2Fnginx-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dunwu%2Fnginx-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dunwu","download_url":"https://codeload.github.com/dunwu/nginx-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505869,"owners_count":21115354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["load-balancer","nginx","proxy","tutorial"],"created_at":"2024-07-31T09:01:04.676Z","updated_at":"2025-10-07T22:08:38.469Z","avatar_url":"https://github.com/dunwu.png","language":"JavaScript","readme":"# Nginx 极简教程\n\n\u003e 本项目是一个 Nginx 极简教程，目的在于帮助新手快速入门 Nginx。\n\u003e\n\u003e [**examples**](https://github.com/dunwu/nginx-tutorial/tree/master/examples) 目录中的示例模拟了工作中的一些常用实战场景，并且都可以通过脚本一键式启动，让您可以快速看到演示效果。\n\n\u003c!-- TOC depthFrom:2 depthTo:3 --\u003e\n\n- [一、Nginx 简介](#一nginx-简介)\n- [二、Nginx 入门](#二nginx-入门)\n- [三、Nginx 实战](#三nginx-实战)\n  - [Http 反向代理](#http-反向代理)\n  - [Https 反向代理](#https-反向代理)\n  - [负载均衡](#负载均衡)\n  - [网站有多个 webapp 的配置](#网站有多个-webapp-的配置)\n  - [静态站点](#静态站点)\n  - [搭建文件服务器](#搭建文件服务器)\n  - [解决跨域](#解决跨域)\n- [资源](#资源)\n\n\u003c!-- /TOC --\u003e\n\n## 一、Nginx 简介\n\n**什么是 Nginx?**\n\n**Nginx (engine x)** 是一款轻量级的 Web 服务器 、反向代理服务器及电子邮件（IMAP/POP3）代理服务器。\n\n![img](https://raw.githubusercontent.com/dunwu/images/master/cs/web/nginx/nginx.jpg)\n\n**什么是反向代理？**\n\n反向代理（Reverse Proxy）方式是指以代理服务器来接受 internet 上的连接请求，然后将请求转发给内部网络上的服务器，并将从服务器上得到的结果返回给 internet 上请求连接的客户端，此时代理服务器对外就表现为一个反向代理服务器。\n\n![img](https://raw.githubusercontent.com/dunwu/images/master/cs/web/nginx/reverse-proxy.png)\n\n## 二、Nginx 入门\n\n\u003e 详细安装方法请参考：[Nginx 运维](docs/nginx-ops.md)\n\nnginx 的使用比较简单，就是几条命令。\n\n常用到的命令如下：\n\n```batch\nnginx -s stop       快速关闭Nginx，可能不保存相关信息，并迅速终止web服务。\nnginx -s quit       平稳关闭Nginx，保存相关信息，有安排的结束web服务。\nnginx -s reload     因改变了Nginx相关配置，需要重新加载配置而重载。\nnginx -s reopen     重新打开日志文件。\nnginx -c filename   为 Nginx 指定一个配置文件，来代替缺省的。\nnginx -t            不运行，仅仅测试配置文件。nginx 将检查配置文件的语法的正确性，并尝试打开配置文件中所引用到的文件。\nnginx -v            显示 nginx 的版本。\nnginx -V            显示 nginx 的版本，编译器版本和配置参数。\n```\n\n如果不想每次都敲命令，可以在 nginx 安装目录下新添一个启动批处理文件**startup.bat**，双击即可运行。内容如下：\n\n```batch\n@echo off\nrem 如果启动前已经启动nginx并记录下pid文件，会kill指定进程\nnginx.exe -s stop\n\nrem 测试配置文件语法正确性\nnginx.exe -t -c conf/nginx.conf\n\nrem 显示版本信息\nnginx.exe -v\n\nrem 按照指定配置去启动nginx\nnginx.exe -c conf/nginx.conf\n```\n\n如果是运行在 Linux 下，写一个 shell 脚本，大同小异。\n\n## 三、Nginx 实战\n\n我始终认为，各种开发工具的配置还是结合实战来讲述，会让人更易理解。\n\n### Http 反向代理\n\n我们先实现一个小目标：不考虑复杂的配置，仅仅是完成一个 http 反向代理。\n\n`nginx.conf` 配置文件如下：\n\n\u003e **_注：`conf/nginx.conf` 是 nginx 的默认配置文件。你也可以使用 nginx -c 指定你的配置文件_**\n\n```nginx\n#运行用户\n#user somebody;\n\n#启动进程,通常设置成和cpu的数量相等\nworker_processes  1;\n\n#全局错误日志\nerror_log  D:/Tools/nginx-1.10.1/logs/error.log;\nerror_log  D:/Tools/nginx-1.10.1/logs/notice.log  notice;\nerror_log  D:/Tools/nginx-1.10.1/logs/info.log  info;\n\n#PID文件，记录当前启动的nginx的进程ID\npid        D:/Tools/nginx-1.10.1/logs/nginx.pid;\n\n#工作模式及连接数上限\nevents {\n    worker_connections 1024;    #单个后台worker process进程的最大并发链接数\n}\n\n#设定http服务器，利用它的反向代理功能提供负载均衡支持\nhttp {\n    #设定mime类型(邮件支持类型),类型由mime.types文件定义\n    include       D:/Tools/nginx-1.10.1/conf/mime.types;\n    default_type  application/octet-stream;\n\n    #设定日志\n\tlog_format  main  '[$remote_addr] - [$remote_user] [$time_local] \"$request\" '\n                      '$status $body_bytes_sent \"$http_referer\" '\n                      '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n\n    access_log    D:/Tools/nginx-1.10.1/logs/access.log main;\n    rewrite_log     on;\n\n    #sendfile 指令指定 nginx 是否调用 sendfile 函数（zero copy 方式）来输出文件，对于普通应用，\n    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用，可设置为 off，以平衡磁盘与网络I/O处理速度，降低系统的uptime.\n    sendfile        on;\n    #tcp_nopush     on;\n\n    #连接超时时间\n    keepalive_timeout  120;\n    tcp_nodelay        on;\n\n\t#gzip压缩开关\n\t#gzip  on;\n\n    #设定实际的服务器列表\n    upstream zp_server1{\n        server 127.0.0.1:8089;\n    }\n\n    #HTTP服务器\n    server {\n        #监听80端口，80端口是知名端口号，用于HTTP协议\n        listen       80;\n\n        #定义使用www.xx.com访问\n        server_name  www.helloworld.com;\n\n\t\t#首页\n\t\tindex index.html\n\n\t\t#指向webapp的目录\n\t\troot D:\\01_Workspace\\Project\\github\\zp\\SpringNotes\\spring-security\\spring-shiro\\src\\main\\webapp;\n\n\t\t#编码格式\n\t\tcharset utf-8;\n\n\t\t#代理配置参数\n        proxy_connect_timeout 180;\n        proxy_send_timeout 180;\n        proxy_read_timeout 180;\n        proxy_set_header Host $host;\n        proxy_set_header X-Forwarder-For $remote_addr;\n\n        #反向代理的路径（和upstream绑定），location 后面设置映射的路径\n        location / {\n            proxy_pass http://zp_server1;\n        }\n\n        #静态文件，nginx自己处理\n        location ~ ^/(images|javascript|js|css|flash|media|static)/ {\n            root D:\\01_Workspace\\Project\\github\\zp\\SpringNotes\\spring-security\\spring-shiro\\src\\main\\webapp\\views;\n            #过期30天，静态文件不怎么更新，过期可以设大一点，如果频繁更新，则可以设置得小一点。\n            expires 30d;\n        }\n\n        #设定查看Nginx状态的地址\n        location /NginxStatus {\n            stub_status           on;\n            access_log            on;\n            auth_basic            \"NginxStatus\";\n            auth_basic_user_file  conf/htpasswd;\n        }\n\n        #禁止访问 .htxxx 文件\n        location ~ /\\.ht {\n            deny all;\n        }\n\n\t\t#错误处理页面（可选择性配置）\n\t\t#error_page   404              /404.html;\n\t\t#error_page   500 502 503 504  /50x.html;\n        #location = /50x.html {\n        #    root   html;\n        #}\n    }\n}\n```\n\n好了，让我们来试试吧：\n\n1.  启动 webapp，注意启动绑定的端口要和 nginx 中的 `upstream` 设置的端口保持一致。\n2.  更改 host：在 C:\\Windows\\System32\\drivers\\etc 目录下的 host 文件中添加一条 DNS 记录\n\n```\n127.0.0.1 www.helloworld.com\n```\n\n3.  启动前文中 startup.bat 的命令\n4.  在浏览器中访问 www.helloworld.com，不出意外，已经可以访问了。\n\n### Https 反向代理\n\n一些对安全性要求比较高的站点，可能会使用 HTTPS（一种使用 ssl 通信标准的安全 HTTP 协议）。\n\n这里不科普 HTTP 协议和 SSL 标准。但是，使用 nginx 配置 https 需要知道几点：\n\n- HTTPS 的固定端口号是 443，不同于 HTTP 的 80 端口\n- SSL 标准需要引入安全证书，所以在 nginx.conf 中你需要指定证书和它对应的 key\n\n其他和 http 反向代理基本一样，只是在 `Server` 部分配置有些不同。\n\n```nginx\n  #HTTP服务器\n  server {\n      #监听443端口。443为知名端口号，主要用于HTTPS协议\n      listen       443 ssl;\n\n      #定义使用www.xx.com访问\n      server_name  www.helloworld.com;\n\n      #ssl证书文件位置(常见证书文件格式为：crt/pem)\n      ssl_certificate      cert.pem;\n      #ssl证书key位置\n      ssl_certificate_key  cert.key;\n\n      #ssl配置参数（选择性配置）\n      ssl_session_cache    shared:SSL:1m;\n      ssl_session_timeout  5m;\n      #数字签名，此处使用MD5\n      ssl_ciphers  HIGH:!aNULL:!MD5;\n      ssl_prefer_server_ciphers  on;\n\n      location / {\n          root   /root;\n          index  index.html index.htm;\n      }\n  }\n```\n\n### 负载均衡\n\n前面的例子中，代理仅仅指向一个服务器。\n\n但是，网站在实际运营过程中，大部分都是以集群的方式运行，这时需要使用负载均衡来分流。\n\nnginx 也可以实现简单的负载均衡功能。\n\n![img](https://raw.githubusercontent.com/dunwu/images/master/cs/web/nginx/nginx-load-balance.png)\n\n假设这样一个应用场景：将应用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 环境的服务器上。网站域名叫 www.helloworld.com，公网 IP 为 192.168.1.11。在公网 IP 所在的服务器上部署 nginx，对所有请求做负载均衡处理（下面例子中使用的是加权轮询策略）。\n\nnginx.conf 配置如下：\n\n```nginx\nhttp {\n     #设定mime类型,类型由mime.type文件定义\n    include       /etc/nginx/mime.types;\n    default_type  application/octet-stream;\n    #设定日志格式\n    access_log    /var/log/nginx/access.log;\n\n    #设定负载均衡的服务器列表\n    upstream load_balance_server {\n        #weigth参数表示权值，权值越高被分配到的几率越大\n        server 192.168.1.11:80   weight=5;\n        server 192.168.1.12:80   weight=1;\n        server 192.168.1.13:80   weight=6;\n    }\n\n   #HTTP服务器\n   server {\n        #侦听80端口\n        listen       80;\n\n        #定义使用www.xx.com访问\n        server_name  www.helloworld.com;\n\n        #对所有请求进行负载均衡请求\n        location / {\n            root        /root;                 #定义服务器的默认网站根目录位置\n            index       index.html index.htm;  #定义首页索引文件的名称\n            proxy_pass  http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表\n\n            #以下是一些反向代理的配置(可选择性配置)\n            #proxy_redirect off;\n            proxy_set_header Host $host;\n            proxy_set_header X-Real-IP $remote_addr;\n            #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP\n            proxy_set_header X-Forwarded-For $remote_addr;\n            proxy_connect_timeout 90;          #nginx跟后端服务器连接超时时间(代理连接超时)\n            proxy_send_timeout 90;             #后端服务器数据回传时间(代理发送超时)\n            proxy_read_timeout 90;             #连接成功后，后端服务器响应时间(代理接收超时)\n            proxy_buffer_size 4k;              #设置代理服务器（nginx）保存用户头信息的缓冲区大小\n            proxy_buffers 4 32k;               #proxy_buffers缓冲区，网页平均在32k以下的话，这样设置\n            proxy_busy_buffers_size 64k;       #高负荷下缓冲大小（proxy_buffers*2）\n            proxy_temp_file_write_size 64k;    #设定缓存文件夹大小，大于这个值，将从upstream服务器传\n\n            client_max_body_size 10m;          #允许客户端请求的最大单文件字节数\n            client_body_buffer_size 128k;      #缓冲区代理缓冲用户端请求的最大字节数\n        }\n    }\n}\n```\n\n#### 负载均衡策略\n\nNginx 提供了多种负载均衡策略，让我们来一一了解一下：\n\n负载均衡策略在各种分布式系统中基本上原理一致，对于原理有兴趣，不妨参考 [负载均衡](https://dunwu.github.io/blog/pages/98a1c1/)\n\n##### 轮询\n\n```nginx\nupstream bck_testing_01 {\n  # 默认所有服务器权重为 1\n  server 192.168.250.220:8080\n  server 192.168.250.221:8080\n  server 192.168.250.222:8080\n}\n```\n\n##### 加权轮询\n\n```nginx\nupstream bck_testing_01 {\n  server 192.168.250.220:8080   weight=3\n  server 192.168.250.221:8080              # default weight=1\n  server 192.168.250.222:8080              # default weight=1\n}\n```\n\n##### 最少连接\n\n```nginx\nupstream bck_testing_01 {\n  least_conn;\n\n  # with default weight for all (weight=1)\n  server 192.168.250.220:8080\n  server 192.168.250.221:8080\n  server 192.168.250.222:8080\n}\n```\n\n##### 加权最少连接\n\n```nginx\nupstream bck_testing_01 {\n  least_conn;\n\n  server 192.168.250.220:8080   weight=3\n  server 192.168.250.221:8080              # default weight=1\n  server 192.168.250.222:8080              # default weight=1\n}\n```\n\n##### IP Hash\n\n```nginx\nupstream bck_testing_01 {\n\n  ip_hash;\n\n  # with default weight for all (weight=1)\n  server 192.168.250.220:8080\n  server 192.168.250.221:8080\n  server 192.168.250.222:8080\n\n}\n```\n\n##### 普通 Hash\n\n```nginx\nupstream bck_testing_01 {\n\n  hash $request_uri;\n\n  # with default weight for all (weight=1)\n  server 192.168.250.220:8080\n  server 192.168.250.221:8080\n  server 192.168.250.222:8080\n\n}\n```\n\n### 网站有多个 webapp 的配置\n\n当一个网站功能越来越丰富时，往往需要将一些功能相对独立的模块剥离出来，独立维护。这样的话，通常，会有多个 webapp。\n\n举个例子：假如 www.helloworld.com 站点有好几个 webapp，finance（金融）、product（产品）、admin（用户中心）。访问这些应用的方式通过上下文(context)来进行区分:\n\nwww.helloworld.com/finance/\n\nwww.helloworld.com/product/\n\nwww.helloworld.com/admin/\n\n我们知道，http 的默认端口号是 80，如果在一台服务器上同时启动这 3 个 webapp 应用，都用 80 端口，肯定是不成的。所以，这三个应用需要分别绑定不同的端口号。\n\n那么，问题来了，用户在实际访问 www.helloworld.com 站点时，访问不同 webapp，总不会还带着对应的端口号去访问吧。所以，你再次需要用到反向代理来做处理。\n\n配置也不难，来看看怎么做吧：\n\n```nginx\nhttp {\n\t#此处省略一些基本配置\n\n\tupstream product_server{\n\t\tserver www.helloworld.com:8081;\n\t}\n\n\tupstream admin_server{\n\t\tserver www.helloworld.com:8082;\n\t}\n\n\tupstream finance_server{\n\t\tserver www.helloworld.com:8083;\n\t}\n\n\tserver {\n\t\t#此处省略一些基本配置\n\t\t#默认指向product的server\n\t\tlocation / {\n\t\t\tproxy_pass http://product_server;\n\t\t}\n\n\t\tlocation /product/{\n\t\t\tproxy_pass http://product_server;\n\t\t}\n\n\t\tlocation /admin/ {\n\t\t\tproxy_pass http://admin_server;\n\t\t}\n\n\t\tlocation /finance/ {\n\t\t\tproxy_pass http://finance_server;\n\t\t}\n\t}\n}\n```\n\n### 静态站点\n\n有时候，我们需要配置静态站点(即 html 文件和一堆静态资源)。\n\n举例来说：如果所有的静态资源都放在了 `/app/dist` 目录下，我们只需要在 `nginx.conf` 中指定首页以及这个站点的 host 即可。\n\n配置如下：\n\n```nginx\nworker_processes  1;\n\nevents {\n\tworker_connections  1024;\n}\n\nhttp {\n    include       mime.types;\n    default_type  application/octet-stream;\n    sendfile        on;\n    keepalive_timeout  65;\n\n    gzip on;\n    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png;\n    gzip_vary on;\n\n    server {\n\t\tlisten       80;\n\t\tserver_name  static.zp.cn;\n\n\t\tlocation / {\n\t\t\troot /app/dist;\n\t\t\tindex index.html;\n\t\t\t#转发任何请求到 index.html\n\t\t}\n\t}\n}\n```\n\n然后，添加 HOST：\n\n127.0.0.1 static.zp.cn\n\n此时，在本地浏览器访问 static.zp.cn ，就可以访问静态站点了。\n\n### 搭建文件服务器\n\n有时候，团队需要归档一些数据或资料，那么文件服务器必不可少。使用 Nginx 可以非常快速便捷的搭建一个简易的文件服务。\n\nNginx 中的配置要点：\n\n- 将 autoindex 开启可以显示目录，默认不开启。\n- 将 autoindex_exact_size 开启可以显示文件的大小。\n- 将 autoindex_localtime 开启可以显示文件的修改时间。\n- root 用来设置开放为文件服务的根路径。\n- charset 设置为 `charset utf-8,gbk;`，可以避免中文乱码问题（windows 服务器下设置后，依然乱码，本人暂时没有找到解决方法）。\n\n一个最简化的配置如下：\n\n```nginx\nautoindex on;# 显示目录\nautoindex_exact_size on;# 显示文件大小\nautoindex_localtime on;# 显示文件时间\n\nserver {\n    charset      utf-8,gbk; # windows 服务器下设置后，依然乱码，暂时无解\n    listen       9050 default_server;\n    listen       [::]:9050 default_server;\n    server_name  _;\n    root         /share/fs;\n}\n```\n\n### 解决跨域\n\nweb 领域开发中，经常采用前后端分离模式。这种模式下，前端和后端分别是独立的 web 应用程序，例如：后端是 Java 程序，前端是 React 或 Vue 应用。\n\n各自独立的 web app 在互相访问时，势必存在跨域问题。解决跨域问题一般有两种思路：\n\n1.  **CORS**\n\n在后端服务器设置 HTTP 响应头，把你需要允许访问的域名加入 `Access-Control-Allow-Origin` 中。\n\n2.  **jsonp**\n\n把后端根据请求，构造 json 数据，并返回，前端用 jsonp 跨域。\n\n这两种思路，本文不展开讨论。\n\n需要说明的是，nginx 根据第一种思路，也提供了一种解决跨域的解决方案。\n\n举例：www.helloworld.com 网站是由一个前端 app ，一个后端 app 组成的。前端端口号为 9000， 后端端口号为 8080。\n\n前端和后端如果使用 http 进行交互时，请求会被拒绝，因为存在跨域问题。来看看，nginx 是怎么解决的吧：\n\n首先，在 enable-cors.conf 文件中设置 cors ：\n\n```nginx\n# allow origin list\nset $ACAO '*';\n\n# set single origin\nif ($http_origin ~* (www.helloworld.com)$) {\n  set $ACAO $http_origin;\n}\n\nif ($cors = \"trueget\") {\n\tadd_header 'Access-Control-Allow-Origin' \"$http_origin\";\n\tadd_header 'Access-Control-Allow-Credentials' 'true';\n\tadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n\tadd_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';\n}\n\nif ($request_method = 'OPTIONS') {\n  set $cors \"${cors}options\";\n}\n\nif ($request_method = 'GET') {\n  set $cors \"${cors}get\";\n}\n\nif ($request_method = 'POST') {\n  set $cors \"${cors}post\";\n}\n```\n\n接下来，在你的服务器中 `include enable-cors.conf` 来引入跨域配置：\n\n```nginx\n# ----------------------------------------------------\n# 此文件为项目 nginx 配置片段\n# 可以直接在 nginx config 中 include（推荐）\n# 或者 copy 到现有 nginx 中，自行配置\n# www.helloworld.com 域名需配合 dns hosts 进行配置\n# 其中，api 开启了 cors，需配合本目录下另一份配置文件\n# ----------------------------------------------------\nupstream front_server{\n  server www.helloworld.com:9000;\n}\nupstream api_server{\n  server www.helloworld.com:8080;\n}\n\nserver {\n  listen       80;\n  server_name  www.helloworld.com;\n\n  location ~ ^/api/ {\n    include enable-cors.conf;\n    proxy_pass http://api_server;\n    rewrite \"^/api/(.*)$\" /$1 break;\n  }\n\n  location ~ ^/ {\n    proxy_pass http://front_server;\n  }\n}\n```\n\n到此，就完成了。\n\n## 资源\n\n- [Nginx 的中文维基](http://tool.oschina.net/apidocs/apidoc?api=nginx-zh)\n- [Nginx 开发从入门到精通](http://tengine.taobao.org/book/index.html)\n- [nginx-admins-handbook](https://github.com/trimstray/nginx-admins-handbook)\n- [nginxconfig.io](https://nginxconfig.io/) - 一款 Nginx 配置生成器\n","funding_links":[],"categories":["JavaScript","IT 相关","资源"],"sub_categories":["DevOps","教程"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdunwu%2Fnginx-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdunwu%2Fnginx-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdunwu%2Fnginx-tutorial/lists"}