怎么在Nginx中配置反向代理并清除缓存
更新:HHH   时间:2023-1-7


本篇文章为大家展示了怎么在Nginx中配置反向代理并清除缓存,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一. Nginx 配置

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

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

#user nobody;

worker_processes 1;

 

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

 

#pid    logs/nginx.pid;

 

 

events {

  worker_connections 1024;

}

 

 

http {

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '

           '$status $body_bytes_sent "$http_referer" '

           '"$http_user_agent" "$http_x_forwarded_for"'

       '$upstream_addr $upstream_status $request_time $upstream_response_time';

 

 

  access_log /var/log/nginx/access.log main;

  error_log  /var/log/nginx/error.log;

 

  server_tokens off;

  include    mime.types;

  default_type application/octet-stream;

  sendfile    on;

  tcp_nopush   on;

  client_max_body_size 10m;

  client_body_buffer_size 128k;

 

  proxy_connect_timeout 300;

  proxy_send_timeout 300;

  proxy_read_timeout 300;

  proxy_buffer_size 64k;

  proxy_buffers 4 512k;

  proxy_busy_buffers_size 512k;

  proxy_temp_file_write_size 512k;

  proxy_temp_path  /data/nginx/proxy_temp;

  proxy_cache_path /data/nginx/proxy_cache levels=1:2 keys_zone=cache_one:2000m inactive=3d max_size=500g;

 

  proxy_redirect off;

  proxy_set_header Host $host;

  proxy_set_header X-Real-IP $remote_addr;

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

  gzip on;

  gzip_min_length 1k;

  gzip_buffers 4 16k;

  gzip_http_version 1.1;

  gzip_comp_level 2;

  gzip_types text/plain application/x-javascript text/css application/xml;

  gzip_vary on;

 

  upstream go {

    server www.cnblogs.com:80 weight=4;

    #server 42.121.252.58:80 weight=4;

  }

 

  server {

 

      listen  192.168.55.133:80;

      #server_name www.cnblogs.com;

      access_log /var/log/nginx/go.access.log main;

      error_log  /var/log/nginx/go.error.log error;

 

 

      location / {

          proxy_cache cache_one;

          #proxy_cache_valid 200 304 301 302 2h;

          #proxy_cache_valid any 2h;

          #expires 1d;

          add_header X-Cache $upstream_cache_status;

          proxy_pass http://go;

          proxy_cache_key  $uri$is_args$args;

      }

 

      location ~ /purge(/.*) {

        allow       127.0.0.1;

        allow       192.168.55.0/24;

        deny        all;

        proxy_cache_purge cache_one $1$is_args$args;

    }

 

  }

}

二. 测试

1.首次访问

 2.刷新一次,也就是第二次访问

3.缓存清理

上述内容就是怎么在Nginx中配置反向代理并清除缓存,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。

返回服务器教程...