@Nginx学习笔记
Nginx的安装
目前工作中使用Nginx,可以完成反向代理、静态资源访问、负载均衡的任务,非常轻便稳定。如下,将介绍一下Nginx的安装与使用
安装
准备
$ yum install -y gcc gcc-c++ (安装gcc)
$ cd /usr/local/
$ wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
$ tar -zxvf pcre-8.36.tar.gz
$ cd pcre-8.36
$ ./configure
$ make && make install (安装pcre)
$ cd /usr/local/
$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ cd openssl-1.0.1j
$ ./config
$ make && make install (安装SSL)
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ ./configure
$ make && make install (安装zlib)
安装ngix
$ cd /usr/local/
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
$ tar -zxvf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0
$ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
$ make && make install
启动
$ /usr/local/nginx/sbin/nginx
$ /usr/local/nginx/sbin/nginx –s reload
$ /usr/local/nginx/sbin/nginx –s stop
$ /usr/local/nginx/sbin/nginx –t
$ pkill nginx
配置
服务器功能:
server {
listen 80;
server_name localhost; //配置server_name,指定目标IP
root html;
index index.html index.htm;
}
静态资源访问:
location /file/ {
alias /home/file/myfile/;
access_log logs/access.log;
}
服务转发:
location / {
proxy_pass 192.168.2.21:8080
}
负载均衡
upstream myserver; {
ip_hash;
server 192.168.2.21:8080;
server 192.168.2.22:8080
server 192.168.2.23:8080
server 192.168.2.24:8080
}
location / {
proxy_pass http://myserver;
}