1.安装编译环境
apt-get install build-essential git gcc g++ make
2.安装 lua
apt-get install liblua5.1-0-dev liblua50-dev liblualib50-dev
3.安装 luajit
wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz tar -zxvf LuaJIT-2.0.3.tar.gz cd LuaJIT-2.0.3 make PREFIX=/usr/local make install
4.下载 nginx
因为 lua-nginx-module 只支持 Lua 5.1 或者 LuaJIT 2.0/2.1 版本,nginx 目前最高支持 1.9.3版本,所以需要设置 http_spdy_module 模块,1.9.5版本以上就可以支持
http_v2_module 模块了。
wget "http://nginx.org/download/nginx-1.9.3.tar.gz" wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz" wget "https://www.openssl.org/source/openssl-1.0.1j.tar.gz" wget "http://zlib.net/zlib-1.2.8.tar.gz" git clone https://github.com/cuber/ngx_http_google_filter_module git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module git clone https://github.com/openresty/lua-nginx-module.git git clone https://github.com/simpl/ngx_devel_kit.git tar xzvf nginx-1.9.3.tar.gz tar xzvf pcre-8.38.tar.gz tar xzvf openssl-1.0.1j.tar.gz tar xzvf zlib-1.2.8.tar.gz
5.编译安装 nginx
./configure \ --user=root \ --group=root \ --prefix=/usr/local/nginx \ --with-pcre=../pcre-8.38 \ --with-openssl=../openssl-1.0.1j \ --with-zlib=../zlib-1.2.8 \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_spdy_module \ --with-http_gzip_static_module \ --with-ipv6 \ --with-http_sub_module \ --add-module=../ngx_http_google_filter_module \ --add-module=../ngx_http_substitutions_filter_module \ --add-module=../ngx_devel_kit \ --add-module=../lua-nginx-module
参考资料:https://github.com/openresty/lua-nginx-module/tree/master
6.编辑 nginx.conf
#user nobody;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 51200;
multi_accept on;
}
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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server {
listen 80;
server_name scholar.xxx.com;
rewrite ^(.*) https://scholar.xxx.com$1 permanent;
}
server {
listen 443 spdy;
server_name scholar.xxx.com;
ssl on;
ssl_certificate /usr/local/nginx/conf/xxx.crt;
ssl_certificate_key /usr/local/nginx/conf/xxx.key;
ssl_dhparam /usr/local/nginx/conf/dhparam2048.pem;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
ssl_prefer_server_ciphers on;
ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
keepalive_timeout 70;
ssl_buffer_size 1400;
spdy_headers_comp 0;
resolver 8.8.8.8;
location / {
set $upstream "";
rewrite_by_lua '
local upstreams = {
"http://a.xxx.com",
"http://b.xxx.com",
"http://c.xxx.com"
}
ngx.var.upstream=upstreams[ math.random( #upstreams ) ]
';
#resolver 8.8.8.8;
proxy_buffering off;
proxy_redirect off;
proxy_set_header Accept-Encoding "";
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass $upstream;
}
}
}
7.注意事项
我在一台 VPS 上很容易就安装完成了,但是在另外一台 VPS 就死活是 502 错误,找了好久才知道是 DNS 解析的问题,需要添加 resolver 8.8.8.8; 才能正常使用。
解答问题是在这里找到的:
http://stackoverflow.com/questions/17685674/nginx-proxy-pass-with-remote-addr
It seems a bit strange that nginx is failing to resolve the domain name at runtime rather than at configuration time (since the domain name is hard coded). Adding a resolver declaration to the location block usually fixes dns issues experienced at runtime. So your location block might look like:
location ^~ /freegeoip/ {
#use google as dns
resolver 8.8.8.8;
proxy_pass http://freegeoip.net/json/$remote_addr;
}
而我将 resolver 8.8.8.8; 提到 location / 前面去了,作用是一样的。到此为止,将 Google 搜索和 Google Scholar 都配置完成了,解决了在学习中查找资料的问题。由于某同学的建议不要在博客上大肆宣传,所以这将部分配置写出,防止遗忘。
Recent Comments