[root@localhost /]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
[root@localhost src]# wget http://nginx.org/download/nginx-1.13.3.tar.gz
[root@localhost src]# tar xvf nginx-1.13.3.tar.gz
[root@localhost src]# cd nginx-1.13.3
为了后续准备我们另外下载2个插件模块:nginx_upstream_check_module-0.3.0.tar.gz —— 检查后端服务器的状态,nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建议在/usr/local/src下解压后将目录重命名为nginx-sticky-module-ng-1.2.5)* —— 后端做负载均衡解决session sticky问题*。
# 下载文件并重新命名
[root@localhost src]# wget -O "nginx_upstream_check_module-0.3.0.tar.gz" https://codeload.github.com/yaoweibin/nginx_upstream_check_module/tar.gz/v0.3.0
[root@localhost nginx-1.13.3]# wget -O "nginx-sticky-module-ng-1.2.5.zip" https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
# 解压文件并重命名目录
[root@localhost src]# tar -xvf nginx_upstream_check_module-0.3.0.tar.gz
[root@localhost src]# unzip nginx-sticky-module-ng-1.2.5.zip
[root@localhost src]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky-module-ng-1.2.5
[root@localhost nginx-1.13.3]# ./configure \
--prefix=/usr/local/nginx \
--with-pcre \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--pid-path=/usr/local/nginx/run/nginx.pid \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--add-module=../nginx_upstream_check_module-0.3.0 \
--add-module=../nginx-sticky-module-ng-1.2.5
[root@localhost nginx-1.13.3]# make
[root@localhost nginx-1.13.3]# make install
make的地方有一个小技巧,如果服务器是双核,可以通过-j2来指定用双核进行编译,-j4代表4核编译。
常用编译选项说明
[root@localhost conf]# groupadd nginx
[root@localhost conf]# useradd -g nginx nginx
nginx默认的配置文件也是可以使nginx正常启动。这里另外提供一份配置文件的示例。
下面的nginx.conf简单的实现nginx在前端做反向代理服务器的例子,处理js、png等静态文件,jsp等动态请求转发到其它服务器
[root@localhost conf]# cat /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/run/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
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;
sendfile on;
# tcp_nopush on;
keepalive_timeout 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
}
}
## 其它虚拟主机,server 指令开始
}
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
Nginx安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建nginx文件,并更改权限其即可。
[root@localhost src]# vim /etc/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start(){
if [ -e $nginx_pid ]; then
echo "nginx already running..."
exit 1
fi
echo -n $"Starting $prog:"
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop(){
echo -n $"Stopping $prog:"
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
#reload nginx service functions.
reload(){
echo -n $"Reloading $proc:"
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
[root@localhost nginx-1.13.3]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
[root@localhost nginx-1.13.3]# source /etc/profile
[root@localhost logs]# systemctl status nginx
● nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: active (running) since 四 2017-07-20 17:00:44 CST; 39min ago
Docs: man:systemd-sysv-generator(8)
Process: 2968 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
Main PID: 2971 (nginx)
CGroup: /system.slice/nginx.service
├─2971 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
└─2973 nginx: worker process
7月 20 17:00:44 localhost systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
7月 20 17:00:44 localhost nginx[2968]: /etc/rc.d/init.d/nginx: 第 26 行:[: =: 期待一元表达式
7月 20 17:00:44 localhost nginx[2968]: Starting nginx:[ 确定 ]
7月 20 17:00:44 localhost systemd[1]: PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
问题1、/etc/rc.d/init.d/nginx: 第 26 行:[: =: 期待一元表达式
决解方法:[ “$yn” != “” ]变量那加引号。
问题2、PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
决解方法:
1.建立nginx运行的组和用户,并将相应的目录分配给新建的组和用户。
2.修改目录权限为可读可写:chmod -R 766 logs/
7月 20 16:17:51 localhost nginx[2671]: Starting nginx:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
7月 20 16:17:51 localhost nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
7月 20 16:17:52 localhost nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
7月 20 16:17:52 localhost nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
7月 20 16:17:53 localhost nginx[2671]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
7月 20 16:17:53 localhost nginx[2671]: nginx: [emerg] still could not bind()
7月 20 16:17:53 localhost nginx[2671]: [失败]
7月 20 16:17:53 localhost systemd[1]: nginx.service: control process exited, code=exited status=1
7月 20 16:17:53 localhost systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
查看端口是否被占用