MENU

编译安装nginx|配置反代和缓存的记录

自从放弃了gre隧道,从某个猥琐的小伙伴那里搞来的ip也不能闲着,所以直接配置成反代吧,以前反代嫌麻烦,一般都是网上直接cpoy来用,到了后来干脆就直接用haproxy中继tcp链接了,过了一段时间才发现这样只是达到了减速的效果,没啥卵用,现在准备配置下科学的反代.

下载和编译nginx

这里的configure参数是根据宝塔面板编译nginx1.12时的参数修改的.

wget http://nginx.org/download/nginx-1.13.3.tar.gz
tar zxvf nginx-1.13.3.tar.gz
cd nginx-1.13.3
./configure --user=www --group=www --prefix=/usr/local/nginx --with-openssl=/root/openssl-1.1.0f --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --add-module=/root/ngx_cache_purge-2.3
make -j 2
make install

这里的make -j 2指的是使用2个线程进行编译,速度更快一点,根据自己的cpu和内存可以适当的增加,如果编译时发现killed字样需要按ctrl+c退出编译,然后减小线程或者不使用线程.

创建www用户和组以及一些目录

这里可以忽略,但我想弄的'正规'一点.

mkdir /data
mkdir /data/nginx
mkdir /data/nginx/cache
mkdir /data/nginx/ssl
mkdir /usr/local/nginx/conf/vhost
chmod -R 755 /data
chmod -R 755 /usr/local/nginx
groupadd www
useradd -g www www
chown -R www:www /data
chown -R www:www /usr/local/nginx

反代和缓存的配置

首先是/usr/local/nginx/conf/nginx.conf的修改,这里我根据我的需求删掉了很多东西和所有注释.

user  www www;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    include vhost/*.conf;
}

然后是最关键的反代和缓存部分,路径是/usr/local/nginx/conf/vhost/blog.ni-co.moe.conf

proxy_cache_path  /data/nginx/cache/one  levels=1:2   keys_zone=one:100m max_size=256m;
proxy_cache_key  "$host$request_uri";
server
{
    listen 80;
    listen 443 ssl;
    listen [::]:80;
    listen [::]:443 ssl;
    server_name blog.ni-co.moe;
    ssl_certificate    /data/ssl/blog.ni-co.moe/fullchain.pem;
    ssl_certificate_key    /data/ssl/blog.ni-co.moe/privkey.pem;
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    location / {
        proxy_pass  https://210.x.x.x;
 
        #Proxy Settings
        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;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
        proxy_pass  https://210.x.x.x;
        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 one;
        proxy_cache_valid  200 302  7d;
        proxy_cache_valid  404      1m;
        add_header X-Cache $upstream_cache_status;
    }
    location ~ /purge(/.*) {
        allow 210.x.x.x;
        deny all;
        proxy_cache_purge one $host$1$is_args$args;
    }
}

这里如果不使用ipv6可以删掉两个ipv6的监听,另外需要注意反代的协议类型,我源站也是使用的https所以proxy_pass也使用的https://,如果源站使用的http请换回http,还有最下面的allow,这是刷新缓存白名单ip,使用白名单ip访问https://domain.com/purge/xxx.jpg才可以刷新缓存,如果填写0.0.0.0则是所有ip都可以进行刷新操作,不推荐这样做.

nginx启动

使用刚刚的参数,实际上nginx被安装在了/usr/local/nginx/sbin/nginx,这样的话可以使用绝对路径启动nginx

/usr/local/nginx/sbin/nginx -c/usr/local/nginx/conf/nginx.conf

但我更推荐将nginx复制一份到/sbin目录 然后直接启动

cp /usr/local/nginx/sbin/nginx /sbin/nginx
nginx -c/usr/local/nginx/conf/nginx.conf
最后编辑于: 2024 年 08 月 08 日
添加新评论

已有 1 条评论
  1. 有些第三方模块需要另外下载吧。

996.icu 996.icu