Home Nginx反向代理
Post
Cancel

Nginx反向代理

安装Nginx

1
2
3
4
sudo apt update
sudo apt install nginx

sudo systemctl status nginx

打开http://<host>:80即刻看到nginx的欢迎页面

主要配置

确认nginx.conf文件的config配置引入,配置开启websocket

前往目录

1
cd /etc/nginx

打开文件

1
sudo vim nginx.conf

确保没有注释掉config引入

1
2
3
4
http {
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

开启支持websocket

打开文件

1
sudo vim nginx.conf

在http域中增加

1
2
3
4
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

例如

1
2
3
4
5
6
7
8
http {
        #...
        map $http_upgrade $connection_upgrade {
                default upgrade;
                ''      close;
        }
        #...
}

创建端口和路由转发

创建文件

1
sudo vim /etc/nginx/conf.d/server.conf

添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
        # 局域网端口
        listen 80;
        # 局域网ip
        server_name 192.168.0.108;

        # 匹配 http://192.168.0.108:80/test/tv => http://192.168.0.108:8081/tv
        location /test/ {
                proxy_pass http://192.168.0.108:8081/;
        }
        location / {
                proxy_pass http://192.168.0.108:8080/;
        }

        # 匹配 http://192.168.0.108:80/test/tv => http://192.168.0.108:8081/test/tv
        #location /test/ {
        #        proxy_pass http://192.168.0.108:8081;
        #}
}

控制操作

重新加载配置

1
sudo nginx -s reload

启动nginx

1
sudo systemctl start nginx

关闭nginx

1
sudo systemctl stop nginx
This post is licensed under CC BY 4.0 by the author.