1、安装nginx所需环境
① gcc 安装
# yum install gcc-c++
② PCRE pcre-devel 安装
# yum install -y pcre pcre-devel
③ zlib 安装
# yum install -y zlib zlib-devel
④ OpenSSL 安装
# yum install -y openssl openssl-devel
2、安装Nginx
① 下载nginx
# wget -c https://nginx.org/download/nginx-1.12.1.tar.gz
② 解压
# tar -zxvf nginx-1.12.1.tar.gz # cd nginx-1.12.1
③ 使用默认配置
# ./configure
④ 编译、安装
# make # make install
⑤ 启动nginx
# cd /usr/local/nginx/sbin/ # ./nginx 其它命令 # ./nginx -s stop # ./nginx -s quit # ./nginx -s reload# 重启nginx # /usr/local/nginx/sbin/nginx -s reload
⑥ 设置开机启动
# vim /etc/rc.local 添加一行: /usr/local/nginx/sbin/nginx # 设置执行权限 # chmod 755 rc.local
⑦ 查看nginx的版本及模块
/usr/local/nginx/sbin/nginx -V
⑧ 防火墙中打开Nginx端口(默认的 80)
firewall-cmd --zone=public --add-port=80/tcp --permanent //--permanent永久生效,没有此参数防火墙重启便失效
3、配置文件详解
#全局块
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#event块
events {
worker_connections 1024;
}
#http块
http {
#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;
#负载均衡
upstream tomcats {
server 192.168.0.100:8080 weight=2; #2/7
server 192.168.0.101:8080 weight=3; #3/7
server example.com:8080 weight=2; #2/7
}
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#server块
server {
#server全局块
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location块
location / {
root html;
index index.html index.htm;
}
#alias 别名配置,请求URL为“/test1/file”,返回“/usr/local/file” 用于访问文件系统
#location /test1/ {
# alias /usr/local/;
#}
#root 路径配置,请求URL为“/test/file”,返回“/usr/local/test/file” 用于访问文件系统
location /test/ {
root /usr/local/;
}
#fastdfs 与 Nginx配置
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
#代理配置,转发url 请求URL为“/testurl/hello”,返回“http://192.168.1.111:8089/hello”
location /testurl/ {
proxy_pass http://192.168.1.111:8089/;
}
#负载均衡代理 代理上面配置
location /testfzjh/ {
proxy_pass http://tomcats;
}
#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;
# }
#}
##配置 多个nginx配置文件
include /usr/local/nginx/conf.d/*.conf;
}
以上完毕!



