Nginx 反向代理 Demo

php中文网
发布: 2016-08-08 09:19:14
原创
1510人浏览过

关于Nginx的基础理解,淘宝的文档已经介绍很清楚了:http://tengine.taobao.org/book/
以下记录些具体的使用流程,方便日后查看

一. 整体配置步骤:

  1. 安装Nginx。
    1. 安装依赖包,包括 pcre 以及 zlib 依赖。centos7的 root 权限下 yum list 搜索,再安装。
    2. 下载Nginx。./configure ---> make ---> make install.(安装成功看提示就知道安装在哪,一般在/usr/local/nginx
    3. 访问 wget 127.0.0.1 即可看到安装成功。 
  2. 准备一个机子放Web应用,例如我的应用地址为 192.168.1.99.157:8081/springmvc。再准备一个机子(我的安装Nginx的机子的IP为192.168.199.176)安装上述的Nginx(其实你用同一个机子也是可以测试的,用不同的 端口当做不同的机子)
  3. 在安装了Nginx的机子上修改nginx.conf主配置文件。例如如下的配置文件(测试版):
    1. <p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>worker_processes  2;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>events {</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    worker_connections  1024;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>}</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>http {</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    server_tokens off;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span></span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    include       mime.types;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    default_type  application/octet-stream;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span></span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    charset utf-8;   </span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    server_names_hash_bucket_size 128;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    client_header_buffer_size 32k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    large_client_header_buffers 4 32k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    client_max_body_size 300m;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    tcp_nopush on;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    tcp_nodelay on;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    client_body_buffer_size 512k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_connect_timeout 300s;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_read_timeout 300s;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_send_timeout 300s;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_buffer_size 64k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_buffers 4 32k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_busy_buffers_size 64k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_temp_file_write_size 128k;</span></code></p>
                          <div class="aritcle_card">
                              <a class="aritcle_card_img" href="/ai/1584">
                                  <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680266797527.png" alt="钉钉 AI 助理">
                              </a>
                              <div class="aritcle_card_info">
                                  <a href="/ai/1584">钉钉 AI 助理</a>
                                  <p>钉钉AI助理汇集了钉钉AI产品能力,帮助企业迈入智能新时代。</p>
                                  <div class="">
                                      <img src="/static/images/card_xiazai.png" alt="钉钉 AI 助理">
                                      <span>21</span>
                                  </div>
                              </div>
                              <a href="/ai/1584" class="aritcle_card_btn">
                                  <span>查看详情</span>
                                  <img src="/static/images/cardxiayige-3.png" alt="钉钉 AI 助理">
                              </a>
                          </div>
                      <p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    proxy_ignore_client_abort on; </span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip on;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_http_version 1.1;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_min_length 1k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_buffers 4 16k;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_types text/plain application/x-javascript text/css application/xml;     </span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_comp_level 2;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    gzip_vary on;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>                      '$status $body_bytes_sent "$http_referer" '</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>                      '"$http_user_agent" "$http_x_forwarded_for"';</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    upstream apm {</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        server 192.168.199.157:8081;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    }</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span></span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    server {</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        listen  8080;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        server_name 192.168.199.176;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        charset utf-8;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        location /springmvc/ {</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        	proxy_pass http://apm;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>        }</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    }</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    sendfile        on;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>    keepalive_timeout  65;</span></code></p><p><codeLiberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span>}</span></code></p></p>
      登录后复制

  4. 访问 192.168.199.176:8080/springmvc/ 时,成功访问到 192.168.199.157:8081/springmvc 的首页

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Nginx 反向代理 Demo,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号