一 离线安装(推荐)
1 安装依赖库
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make
2. 下载nginx安装包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
官网下载http://nginx.org/download/
3 解压和配置
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx
make && make install
4 创建nginx用户
groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin -M nginx
5 修改配置文件
修改用户为nginx,进程数为cpu核数
vim /usr/local/nginx/conf/nginx.conf
6 启动
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
7 配置开机启动服务
vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
#添加nginx用户后,日志路经要授权
#User=nginx
#Group=nginx
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8 设置开机启动
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl start nginx.service (启动nginx服务)
systemctl stop nginx.service (停止nginx服务)
systemctl enable nginx.service (设置开机自启动)
systemctl disable nginx.service (停止开机自启动)
systemctl status nginx.service (查看服务当前状态)
systemctl restart nginx.service (重新启动服务)
systemctl list-units --type=service (查看所有已启动的服务)
安装参考
https://www.runoob.com/linux/nginx-install-setup.html?spm=a2c4g.11186623.2.19.31059a80v7aaTf
二 rpm 安装
1 下载安装包
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm
官网下载 http://nginx.org/packages/centos/7/x86_64/RPMS/
rpm -ivh nginx-1.8.0-1.el7.ngx.x86_64.rpm
rpm -ql nginx
查看nginx开机服务,添加nginx用户启动
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
#User=nginx
#Group=nginx
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2 启动
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
三 chkconfig 启动脚本
vim /etc/init.d/nginx
chmod +x vim /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
chkconfig --list
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/www/server/nginx/sbin/$NAME
CONFIGFILE=/www/server/nginx/conf/$NAME.conf
PIDFILE=/www/server/nginx/logs/$NAME.pid
ulimit -n 8192
case "$1" in
start)
echo -n "Starting $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" = '' ];then
echo "$NAME is not running."
exit 1
fi
else
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
else
echo "$NAME is stopped"
exit 0
fi
else
echo "$NAME is stopped"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 1
;;
esac
规范化nginx.conf vhost
vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes auto;
error_log /app/logs/nginx/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format json '{"logtimes":"$msec",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"body_bytes_sent":$body_bytes_sent,'
'"bytes_sent":$bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"request_uri":"$scheme://$http_host$request_uri",'
'"request_method":"$request_method",'
'"request_completion":"$request_completion",'
'"request_length":"$request_length",'
'"scheme":"$scheme",'
'"server_protocol":"$server_protocol",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"Content_Length":"$http_Content_Length",'
'"connection_requests":"$connection_requests",'
'"connection":"$connection",'
'"appkey":"$http_appkey",'
'"method":"$http_method",'
'"status":"$status",'
'"X-B3-TraceId":"$http_X_B3_TraceId",'
'"Pinpoint-TraceId":"$http_Pinpoint_TraceID",'
'"ups_status":$upstream_status}';
access_log /app/logs/nginx/access.log json;
sendfile on;
tcp_nopush on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_types text/plain text/css text/javascript application/x-javascript application/javascript application/xml application/x-shockwave-flash application/json;
gzip_vary on;
keepalive_timeout 60s;
client_max_body_size 100M;
client_body_buffer_size 128k;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
#include /usr/local/nginx/conf/conf.d/*.conf;
include /usr/local/nginx/conf/vhost/*.conf;
}
mkdir /usr/local/nginx/conf/vhost/
vim /usr/local/nginx/conf/vhost/up.conf
upstream xxx-order {
server 192.168.67.6:8081 max_fails=2 fail_timeout=2s weight=10;
server 192.168.67.7:8081 max_fails=2 fail_timeout=2s weight=10;
#check interval=3000 rise=2 fall=5 timeout=1000 type=http;
#check_http_send "HEAD /order/info HTTP/1.0\r\n\r\n";
}
vim /usr/local/nginx/conf/vhost/test.conf
server {
listen 8080 default_server;
server_name api.xxx.com;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
underscores_in_headers on;
location / {
return 200 '{"code":50000,"msg":"走到网关后无法请求到后端应用,请联系对应运维人员","success":1}';
}
location /smartorder/ {
proxy_pass http://xxx-order;
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;
}
}