环境
centerOS 7
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_realip_module
make && make install
测试 /usr/local/nginx/sbin/nginx
例:./configure –prefix=/usr/local/nginx-1.6 –with-pcre \
–with-http_stub_status_module –with-http_ssl_module \
–with-http_gzip_static_module –with-http_realip_module \
–add-module=../nginx_upstream_check_module-0.3.0
nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。
–prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
–conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
–user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
–with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
–with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
–with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
–with-http_stub_status_module : 用来监控 Nginx 的当前状态
–with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
–add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
配置 nginx.cnf
`user root;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
pid /usr/local/nginx/nginx.pid;
events {
use epoll;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main ‘
httphost
remote_user [
timelocal]
request ’
‘
status
body_bytes_sent “
httpreferer”′‘”
http_user_agent"
remoteaddr
request_time
upstreamresponsetime
upstream_addr’;
\#access_log logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m; #256k
#
server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
tcp_nopush on;
tcp_nodelay on;
#proxy options
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_next_upstream error timeout invalid_header http_500;
#proxy_next_upstream_tries 3;
#size limits
client_max_body_size 50m;
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;
proxy_temp_path /dev/shm/proxy_temp;
fastcgi_temp_path /dev/shm/fastcgi_temp;
client_body_temp_path /dev/shm/client_body_temp;
include vhosts/*.conf;
}`
在conf目录创建 vhosts目录
增加节点配置文件如
`server {
listen 80;
server_name i.image.com;
access_log /var/log/nginx/access.log ;
error_log /var/log/nginx/error.log;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
location / {
root /data;
expires 7d;
}
#location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
#root /data/static;
# expires 7d;
#}
}`
或
upstream web{
server 127.0.0.1:10004;
#server ;
}
server {
listen 80;
server_name www.xx.com;
access_log /var/log/nginx/access.log ;
error_log /var/log/nginx/error.log;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://web;
break;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /opt/webapp/trade-web;
#expires 7d;
}
}
/usr/local/nginx/sbin/nginx -t 检查
/usr/local/nginx/sbin/nginx -V 显示编译参数
./sbin/nginx -s stop 或 pkill nginx
重启,不会改变启动时指定的配置文件
./sbin/nginx -s reload
或 kill -HUP cat /usr/local/nginx-1.6/logs/nginx.pid