服务器 > 网络 > https

nginx之Http代理和Websocket代理详解

5人参与 2025-03-05 https

1.nginx安装

按照nginx官方的教程,在ubuntu上安装nginx

http://nginx.org/en/linux_packages.html#ubuntu

按照上面的安装,会为我们注册nginx的service,我们可以通过service nginx start来启动nginx

2.nginx配置文件

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

3.nginx常用命令

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

3.1 帮助命令

nginx -?
nginx -h

3.2 快速停止

nginx -s stop

3.3 优雅停止

nginx -s quit

3.4 重新加载配置文件

nginx -s reload

一旦主进程接收到重新加载配置的信号,它将检查新的配置文件的语法有效性,并尝试应用其中的配置。

如果应用新配置成功,主进程将启动新的工作进程,并向旧的工程发送消息,请求将它们关闭。

如果新配置失败,主进程将回滚配置更改并继续使用旧配置。

3.5 重新打开日志文件

nginx -s reopen

3.6 nginx主进程id

nginx主进程的id将写在/usr/local/nginx/logs/var/run目录中的nginx.pid文件中

我们也可以通过以下命令来杀死nginx主进程:

# pid表示主进程id
kill -s quit pid

3.7 查看nginx进程

ps -ax | grep nginx

3.8 以指定的配置文件运行

nginx -c file

4. nginx静态代理

server {
        listen       80; # 监听的端口
        server_name  localhost; # 服务名称,对应的ip或者域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html { # 服务器错误将会被转发到nginx根目录下的html文件夹中的50x.html
            root   html;
        }
    }

5 nginx做代理服务器

localhost:8082/api/test请求转发到http://192.168.1.92:8080/test

proxy_pass http://192.168.1.92:8080/;

结尾的/必须有,否则请求会被转发到http://192.168.1.92:8080/api/test

http {
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

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

}

6.负载均衡

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

6.1 轮询

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

6.2 最少连接

http {
    upstream myapp1 {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

6.3 ip hash

http {
    upstream myapp1 {
        ip_hash;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

6.4 权重

http {
    upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

6.5相关参数

# 设置server的权重值,默认是1
weight=number

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

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

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

# 将server标记为停止
down

7.websocket代理

http {
    upstream backend {
        server srv1.example.com;

    }

    server {
        listen 80;

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header upgrade $http_upgrade;
            proxy_set_header connection $connection_upgrade;
        }
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

Ubuntu安装Nginx全过程(在线安装&源码编译安装)

03-05

Nginx配置系统服务&设置环境变量方式

03-05

Nginx如何实现对城市以及指定IP的访问限制

03-05

Nginx之正向代理与反向代理进阶方式(支持https)

03-05

Nginx配置反向代理服务器实现在https网站中请求http资源

03-07

Minio环境部署过程及如何配置HTTPS域名

03-07

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论