安装
yum install -y pcre
yum install -y pcre-devel
yum install -y openssl
yum install -y openssl-devel
yum install -y zlib
yum install -y gcc
yum安装:
yum install -y nginx
/etc/init.d/nginx status
源码安装:
yum install -y gcc
./configure --with-http_ssl_module
make
make install
源码安装加的参数,可以使能https的代理模式
命令
如果通过源码安装:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果yum安装
/etc/init.d/nginx status
/etc/init.d/nginx stop
/etc/init.d/nginx start
负载均衡 upstream
简单server
用户登陆http://1.2.3.4/test
会自动映射到http://3.3.3.3:8888/
server {
listen 80;
server_name localhost 1.2.3.4;
include test.conf; # 可以在这里写location
}
test.conf中的内容:
location /test/ {
proxy_pass http://3.3.3.3:8888/;
include path_redirect.conf;
}
path_redirect.conf中的内容:
proxy_redirect default;
proxy_redirect http://3.3.3.3:8888/ http://$host/test/;
https server
server {
listen 443 ssl;
server_name localhost 127.0.0.1 default;
ssl on;
ssl_certificate test.crt;
ssl_certificate_key test.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
location /testhttps {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass https://3.3.3.3:9999;
include testhttps.conf;
}
}
testhttps.conf
proxy_redirect default;
proxy_redirect https://3.3.3.3:9999/ https://$host/;
其中key文件的生成,可以参考如下:
openssl genrsa -des3 -out test.key 1024
openssl req -new -key test.key -out test.csr
cp test.key test.key.org
openssl rsa -in test.key.org -out test.key
openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
FAQ
不支持的命令
[root@vm36 nginx]# /etc/init.d/nginx restart
nginx: [emerg] unknown directive "proxy_cookie_domain" in /etc/nginx/account/account_path.conf:14
nginx: configuration file /etc/nginx/nginx.conf test failed
yum安装后,要看清楚nginx的版本,上面错误是由于版本太低,不支持proxy_cookie_domain
也会出现其他类似nginx: [emerg] unknown directive
的错误,有些是因为依赖库没安装全导致