说到反向代理,肯定是有上游服务。 配置一个静态网站。 ``` 确定配置文件:sudo nano /etc/nginx/sites-available/default server { listen 127.0.0.1:5008; //也就是只针对本机开放 server_name other; location / { alias /var/www/publishapplication/d1.xyz/html/; autoindex on; index index.html; set $limit_rate 1k; try_files $uri $uri/ /index.html; } } 把原先的进程停掉: nginx -s stop 重新打开:nginx ``` 设置反向代理 ``` 配置文件:sudo nano /etc/nginx/sites-available/default upstream local { server 127.0.0.1:5008; } server { listen 80; server_name demo.com *.demo.com; location / { #root /var/www/html; #index index.html; #try_files $uri $uri/ /index.html; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;//告诉上游服务器,从远端与Nginx的TCP连接中获取 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://local; } } server { listen 127.0.0.1:5008; server_name other; location / { alias /var/www/publishapplication/d1.xyz/html/; autoindex on; index index.html; set $limit_rate 1k; try_files $uri $uri/ /index.html; } } ``` 设置缓存。也就是把上游服务器的内容先缓存起来一段时间。 ``` 配置文件:sudo nano /etc/nginx/nginx.conf http { //10m的共享内存 //名称是my_cache proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; } 配置文件:sudo nano /etc/nginx/sites-available/default upstream local { server 127.0.0.1:5008; } server { listen 80; server_name _; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache my_cache; proxy_cache_key #host$uri$is_args$args; proxy_cache_valid 200 304 303 ld; proxy_pass http://local; } } ```