爬行的蜗牛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: golang Linux PHP
查看: 1327|回复: 0

反向代理

[复制链接]

83

主题

31

回帖

780

积分

管理员

积分
780
发表于 2023-9-30 19:43:41 | 显示全部楼层 |阅读模式
  1. server
  2. {
  3.     listen 443 ssl http2 reuseport;
  4.     #http2 表示启用http2协议,http2一般需要openssl 1.0.2+以上版本支持,大部分linux系统需要自行编译nginx作为支持,ubuntu16.04可以直接支持。

  5.     listen 80 reuseport;
  6.     #同时监听80端口。
  7.     #reuseport提高nginx性能,有兴趣可以搜索引擎

  8.     location ~* .(conf|sql|bak)$ {
  9.         deny all;
  10.     }

  11.     ssl_certificate ssl/p.run.la.pem;
  12.     ssl_certificate_key ssl/p.run.la.key;

  13.     ssl_certificate ssl/p.run.la.ecdsa.pem;
  14.     ssl_certificate_key ssl/p.run.la.ecdsa.key;
  15.     #配置双证书,一个是rsa算法的证书,一个是ecc,ecc算法效率更高,但是并不支持某些老的系统。

  16.     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  17.     #配置加密协议,如果要支持ie6还需要加上SSLv3,但是安全性会降低

  18.     ssl_session_timeout      1d;
  19.     #缓存时间

  20.     ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
  21.     #配置https加密算法,chacha20对称算法消耗更少,对于移动端更友好,但是需要自行编译nginx,打Cloudflare 补丁。

  22.     ssl_prefer_server_ciphers on;


  23.     ssl_session_cache        shared:SSL:50m;

  24.     ssl_session_tickets      on;
  25.     #配置ssl缓存

  26.     ssl_stapling             on;
  27.     #开启OCSP,因为Let's Encryptc证书的原因,我开启ocsp无需更多设置。

  28.     server_name p.run.la; #绑定的域名

  29.     if ($request_method !~ ^(GET|HEAD|POST)$ ) {
  30.         return    444;
  31.     }

  32.     if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
  33.     {
  34.         return 403;
  35.     }
  36.     #屏蔽搜索引擎

  37.     resolver                 8.8.8.8 8.8.4.4  valid=600s;
  38.     #设置域名解析的dns服务器
  39.     resolver_timeout         10s;
  40.     #设置dns解析超时时间。

  41.     location ~ ^/(repo\.mongodb\.com|repo\.mysql\.com|www\.debian\.org|deb\.debian\.org|security\.debian\.org|cdn-fastly\.deb.debian\.org|nginx\.org|github\.com|codeload\.github\.com|yum\.dockerproject\.org)(\/.*)$ {
  42.         #需要代理的域名正则,避免有人用来代理某些被墙的网站,导致反向代理被gfw误杀,会的人自己改改就可以反向代理所有网站了,其实在通过sub_filter替换链接可以做到访问大部分网站,但是当初有份写好的,因为某VPS商,导致数据丢了,有兴趣的可以自己试着填一下。
  43.         proxy_set_header  X-Real-IP  $remote_addr;
  44.         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  45.         #将用户ip放到请求协议头内,一般用来告诉后端服务器,是谁访问的网站。
  46.         proxy_set_header Accept-Encoding "";
  47.         #告诉被代理网站,不要压缩内容,否则sub_filter会失效。
  48.         proxy_set_header Connection "";
  49.         #启用http1.1协议
  50.         proxy_http_version 1.1;
  51.         #同上
  52.         proxy_connect_timeout    10s;
  53.         #设置连接超时
  54.         proxy_read_timeout       10s;
  55.         #设置读取超时
  56.         proxy_set_header Host $1;
  57.         #设置host,域名
  58.         proxy_redirect ~^(http:\/\/|https:\/\/)?(.*)$ $1$server_name/$2;
  59.         #将原301跳转,重新跳转回本域名。

  60.         sub_filter 'src="/' 'src="/$1/';
  61.         sub_filter 'src="http://$1' 'src="http://$server_name/$1';
  62.         sub_filter 'src="https://$1' 'src="https://$server_name/$1';
  63.         sub_filter 'src="//$1' 'src="//$server_name/$1';
  64.         #替换网页内链接地址。

  65.         sub_filter 'href="/' 'href="/$1/';
  66.         sub_filter 'href="http://$1' 'href="http://$server_name/$1';
  67.         sub_filter 'href="https://$1' 'href="https://$server_name/$1';
  68.         sub_filter 'href="//$1' 'href="//$server_name/$1';
  69.         #同上

  70.         sub_filter 'action="/' 'action="/$1/';
  71.         sub_filter 'action="http://$1' 'action="http://$server_name/$1';
  72.         sub_filter 'action="https://$1' 'action="https://$server_name/$1';
  73.         sub_filter 'action="//$1' 'action="//$server_name/$1';
  74.         #同上

  75.         sub_filter_once off;
  76.         #替换多次

  77.         proxy_hide_header Strict-Transport-Security;
  78.         #隐藏被代理网站返回回来的协议头“Strict-Transport-Security”,避免启动hsts,具体搜索引擎hsts

  79.         set $query_mark "";
  80.         if ($query_string != "") {
  81.             set $query_mark "?${query_string}";
  82.         }
  83.         #因为nginx并不匹配url后面的?参数,而是使用“$query_string”储存。
  84.         proxy_pass $scheme://$1$2${query_mark};
  85.         #$scheme是当前访问的协议,http、https
  86.     }

  87.     root  /home/wwwroot/p.run.la;
  88.     #设置目录。
  89. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表