• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    nginx之Http代理和Websocket代理详解

    发布者: 福建二哥 | 发布时间: 2025-8-16 17:37| 查看数: 103| 评论数: 0|帖子模式

    1.nginx安装

    按照nginx官方的教程,在Ubuntu上安装nginx
    1. http://nginx.org/en/linux_packages.html#Ubuntu
    复制代码
    按照上面的安装,会为我们注册nginx的service,我们可以通过
    1. service nginx start
    复制代码
    来启动nginx

    2.nginx配置文件

    默认的配置文件是:
    1. nginx.conf
    复制代码
    ,一般存放的位置是
    1. /usr/local/nginx/conf
    复制代码
    ,
    1. /etc/nginx
    复制代码
    , or
    1. /usr/local/etc/nginx
    复制代码
    .

    3.nginx常用命令

    nginx启动之后,我们可以使用
    1. -s
    复制代码
    参数来执行一些控制命令

    3.1 帮助命令
    1. nginx -?
    2. nginx -h
    复制代码
    3.2 快速停止
    1. nginx -s stop
    复制代码
    3.3 优雅停止
    1. nginx -s quit
    复制代码
    3.4 重新加载配置文件
    1. nginx -s reload
    复制代码
    一旦主进程接收到重新加载配置的信号,它将检查新的配置文件的语法有效性,并尝试应用其中的配置。
    如果应用新配置成功,主进程将启动新的工作进程,并向旧的工程发送消息,请求将它们关闭。
    如果新配置失败,主进程将回滚配置更改并继续使用旧配置。

    3.5 重新打开日志文件
    1. nginx -s reopen
    复制代码
    3.6 nginx主进程id

    nginx主进程的id将写在
    1. /usr/local/nginx/logs
    复制代码
    1. /var/run
    复制代码
    目录中的
    1. nginx.pid
    复制代码
    文件中
    我们也可以通过以下命令来杀死nginx主进程:
    1. # pid表示主进程id
    2. kill -s quit pid
    复制代码
    3.7 查看nginx进程
    1. ps -ax | grep nginx
    复制代码
    3.8 以指定的配置文件运行
    1. nginx -c file
    复制代码
    4. nginx静态代理
    1. server {
    2.         listen       80; # 监听的端口
    3.         server_name  localhost; # 服务名称,对应的ip或者域名

    4.         #charset koi8-r;

    5.         #access_log  logs/host.access.log  main;

    6.         location / { # 如果访问localhost,请求将会被转发到nginx根目录下的html文件夹中的index.html或者index.htm
    7.             root   html;
    8.             index  index.html index.htm;
    9.         }

    10.         #error_page  404              /404.html;

    11.         # redirect server error pages to the static page /50x.html
    12.    
    13.         error_page   500 502 503 504  /50x.html;
    14.         location = /50x.html { # 服务器错误将会被转发到nginx根目录下的html文件夹中的50x.html
    15.             root   html;
    16.         }
    17.     }
    复制代码
    5 nginx做代理服务器

    1. localhost:8082/api/test
    复制代码
    请求转发到
    1. http://192.168.1.92:8080/test
    复制代码
    1. proxy_pass http://192.168.1.92:8080/;
    复制代码
    结尾的
    1. /
    复制代码
    必须有,否则请求会被转发到
    1. http://192.168.1.92:8080/api/test
    复制代码
    1. http {
    2.     sendfile        on;
    3.     #tcp_nopush     on;

    4.     #keepalive_timeout  0;
    5.     keepalive_timeout  65;

    6.         server {
    7.                 listen 8082;
    8.                 server_name localhost;
    9.                 location /api/ {
    10.                         proxy_pass http://192.168.1.92:8080/;# 这个结尾的/必须有,否则请求会被代理到http://192.168.1.92:8080/api/test
    11.                 }
    12.         }

    13. }
    复制代码
    6.负载均衡

    负载均衡的策略有轮询、最少连接数、iphash、权重。默认使用的负载均衡策略是轮询。

    6.1 轮询
    1. http {
    2.     upstream myapp1 {
    3.         server srv1.example.com;
    4.         server srv2.example.com;
    5.         server srv3.example.com;
    6.     }

    7.     server {
    8.         listen 80;

    9.         location / {
    10.             proxy_pass http://myapp1;
    11.         }
    12.     }
    13. }
    复制代码
    6.2 最少连接
    1. http {
    2.     upstream myapp1 {
    3.         least_conn;
    4.         server srv1.example.com;
    5.         server srv2.example.com;
    6.         server srv3.example.com;
    7.     }

    8.     server {
    9.         listen 80;

    10.         location / {
    11.             proxy_pass http://myapp1;
    12.         }
    13.     }
    14. }
    复制代码
    6.3 ip hash
    1. http {
    2.     upstream myapp1 {
    3.         ip_hash;
    4.         server srv1.example.com;
    5.         server srv2.example.com;
    6.         server srv3.example.com;
    7.     }

    8.     server {
    9.         listen 80;

    10.         location / {
    11.             proxy_pass http://myapp1;
    12.         }
    13.     }
    14. }
    复制代码
    6.4 权重
    1. http {
    2.     upstream myapp1 {
    3.         server srv1.example.com weight=3;
    4.         server srv2.example.com;
    5.         server srv3.example.com;
    6.     }

    7.     server {
    8.         listen 80;

    9.         location / {
    10.             proxy_pass http://myapp1;
    11.         }
    12.     }
    13. }
    复制代码
    6.5相关参数
    1. # 设置server的权重值,默认是1
    2. weight=number

    3. # 最大连接数,限制代理服务器的最大连接数,默认值为0,表示没有限制
    4. max_conns=number

    5. # 服务器的不可用时间,当连接失败时
    6. fail_timeout=time

    7. # 将server标记为备用,当主server不可用时,将请求备用server
    8. backup

    9. # 将server标记为停止
    10. down
    复制代码
    7.websocket代理
    1. http {
    2.     upstream backend {
    3.         server srv1.example.com;

    4.     }

    5.     server {
    6.         listen 80;

    7.         location /chat/ {
    8.             proxy_pass http://backend;
    9.             proxy_http_version 1.1;
    10.             proxy_set_header Upgrade $http_upgrade;
    11.             proxy_set_header Connection $connection_upgrade;
    12.         }
    13.     }
    14. }
    复制代码
    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表