0%

linux下Nginx配置根据 url不同进行代理转发

nginx代理

根据 url不同 进行代理转发设置

参考文章

https://www.cnblogs.com/wjoyxt/p/11949943.html

在nginx.conf文件里添加配置:

1
2
3
4
5
6
7
8
##1.myserver1 服务代理
location /myserver1/ {
proxy_pass http://192.168.1.1:8082/;
}
##2.myserver2 服务代理
location /myserver2/ {
proxy_pass http://192.168.1.1:8083/;
}

完整示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

##1.myserver1 服务代理
location /myserver1/ {
proxy_pass http://192.168.1.1:8082/;
}
##2.myserver2 服务代理
location /myserver2/ {
proxy_pass http://192.168.1.1:8083/;
}

#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 {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

文件传输及大小限制

文件参数参数

1
2
3
4
#设置为on表示启动高效传输文件的模式
sendfile on;
#如有需要 gzip也放开即可
gzip on;

文件大小限制

1
2
#上传文件大小限制
client_max_body_size 1024M;

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

#设置为on表示启动高效传输文件的模式
sendfile on;
#tcp_nopush on;

#上传文件大小限制
client_max_body_size 1024M;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

docker下安装nginx

拷贝nginx默认配置文件

默认安装:

1
2
docker run --name nginx -d -p 8090:80 \
-d nginx:latest

拷贝/etc/nginx/nginx.conf到宿主机(0871df886cfc是nginx容器id):

1
docker cp 0871df886cfc:/etc/nginx/nginx.conf /Users/ux/Downloads/copy

映射nginx中的conf、html、logs、conf.d文件夹到宿主机

1.在/Users/ux/Applications/docker/nginx下创建conf、html、logs、conf.d文件夹,
并将上一步拷贝出来的nginx.conf拷贝到conf文件夹下;

2.运行并挂载nginx.conf配置文件、html文件夹、log文件夹:

1
2
3
4
5
docker run --name nginx -d -p 8090:80 \
-v /Users/ux/Applications/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/ux/Applications/docker/nginx/html:/usr/share/nginx/html \
-v /Users/ux/Applications/docker/nginx/logs:/var/log/nginx \
-d nginx:latest

参考文章:

https://blog.csdn.net/qq_42875895/article/details/105027040