Nginx是工作中最常用的反向代理&webserver&负载均衡了,日常使用攒了不少东西,踩了不少坑,记录一下。
1. Nginx安装
我习惯用阿里巴巴依据Nginx做二次开发后的Tengine,Tengine和Nginx在安装方面没什么差别,可以提前下好Tengine的源码包,安装的时候主要的依赖有gcc-c++,pcre,perl,openssl等,这些依赖可以yum安装,也可以手动安装。手动安装的话可以选版本,尤其是openssl的版本太低的话会有不少安全漏洞,后期还得升级,升级后重新编译Nginx,挺麻烦的,能保证一段时间内一次到位最好。
yum安装依赖:
yum -y install gcc-c++ pcre pcre-devel openssl openssl-devel perl-devel perl-ExtUtils-Embed libxml2 libxml2-dev gd-devel zlib-devel
手动安装依赖的话,gcc-c++可以直接yum安装,这个出现版本问题可能性不太大。主要是pcre, perl, openssl这三个。在各自官网下好最近版本的源码包,一个一个安装:
pcre:
./configure --prefix=/usr/local/pcre --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
make && make install
perl:
./Configure -des -Dprefix=/usr/local/perl -Dusethreads -Uversiononly
make && make install
openssl:
./config --prefix=/usr/local/openssl
make && make install
安装Nginx:
./configure --prefix=/u01/scadm/nginx --with-http_stub_status_module --with-openssl=/u01/openssl-1.0.0e --with-pcre=/u01/pcre-8.36
make && make install
*注意指定的是openssl和pcre的安装源目录不是安装好后的目录。
编译的时候可以选择很多模块(--with-blabla),可以按照自己的需求去选择,如果安装时未编译某个模块,后期又需要使用的话,可以重新编译Nginx添加那个模块即可。
安装完成后可以将Nginx加入系统服务,以后就可以直接用service nginx start/stop/restart等命令,比较方便,当然也可以不加,看个人使用习惯而定。
chkconfig --add nginx
chkconfig nginx on
2. Nginx配置
Nginx安装完成后在.${INSTALL_ROOT}/nginx/conf/目录下会有nginx.conf文件,这个文件便是nginx的主配置文件,默认的配置文件更多的是一个样例的作用,有很多功能都是注释掉的(有些注释的就是默认配置),我们需要对其进行一些修改以符合我们自己的需求。
为防止主配置文件过于臃肿,并且方便维护,我们自己的很多配置可以写在另外的配置文件中,并在主配置文件中include。
我比较常用的主配置文件(nginx.conf)如下:
user root;
worker_processes 2; #Nginx开启的子进程数,一般和cpu核数一致。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
#load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#load ngx_http_fastcgi_module.so;
#load ngx_http_rewrite_module.so;
#}
http {
include mime.types;
include reverse-proxy.conf; #我们自己的一些配置,主要是upstream和server信息。
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;
#keepalive_timeout 0;
keepalive_timeout 65;
client_max_body_size 30m; #这个是上传文件大小限制,很多时候nginx无法上传大文件就是因为这个配置没有写。
client_header_timeout 1m;
client_body_timeout 1m;
proxy_connect_timeout 1m;
proxy_read_timeout 1m;
proxy_send_timeout 1m;
#gzip on;
gzip on; #压缩选项,建议开启,会对网站访问速度有帮助,当然也会增加Nginx机器的cpu占用。
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_disable "MSIE [1-6].";
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png;
server {
listen 80; #这段就是一个示例,最基本的一个server配置。
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /nas/webresource;
index index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
#proxy_pass http://127.0.0.1;
#}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
#root html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
#}
#deny access to .htaccess files, if Apache's document root
#concurs with nginx's one
#
#location ~ /.ht {
#deny all;
#}
}
#another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_name somename alias another.alias;
#location / {
#root html;
#index index.html index.htm;
#}
#}
#HTTPS server
#
#server {
#listen 443 ssl;
#server_name localhost;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_cache shared:SSL:1m;
#ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
#location / {
#root html;
#index index.html index.htm;
#}
#}
}
reverse-proxy.conf:
server #每个server为一个站点。
{
listen 80;
server_name a.example.com;
if ($request_method !~* GET|POST) { #拒绝get和post之外的方法访问。
return 403;
}
location / { #/目录为网站根目录。
root /root/to/html;
}
location /a { #网站内每个应用可跳转至后端不同端口的appserver。
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_pass http://ip:port/a/;
}
location /b {
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_pass http://ip:port/b/;
}
access_log logs/access.log;
}
server {
listen 443 ssl;
server_name a.example.com;
ssl_certificate example.crt;
ssl_certificate_key example.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;
if ($request_method !~* GET|POST) {
return 403;
}
location / {
root /root/to/html;
}
location /a {
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_pass http://ip:port/a/;
}
location /b {
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_pass http://ip:port/b/;
}
access_log logs/access.log;
}
server
{
listen 80;
server_name b.example.com;
location / {
root /root/to/html;
}
location ^~ /c/login { #类似登录的部分需要安全访问,跳转https。
rewrite ^(.)$ https://$host$1 permanent;
}
if ($request_method !~ GET|POST) {
return 403;
}
location /c {
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_pass http://ip:port/c/;
}
access_log logs/access.log;
}
server
{
listen 443 ssl;
server_name b.example.com;
ssl_certificate example.crt;
ssl_certificate_key example.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;
if ($request_method !~* GET|POST) {
return 403;
}
location / {
root /root/to/html;
}
location /c {
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_pass http://ip:port/c/;
}
access_log logs/access.log;
}