NGINX|Nginx编译安装与配置优化
#安装指令 基于centosyum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-develgroupadd www
useradd www -g www
wgethttps://nginx.org/download/nginx-1.13.9.tar.gz
tar zxvf nginx-1.13.9.tar.gz
cd nginx-1.13.9
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --user=www --group=www
make && make install
#service服务 启动脚本#centos6启动脚本#! /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# Author:licess
# website:http://lnmp.orgPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAMEset -e
[ -x "$DAEMON" ] || exit 0do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}do_stop() {
kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}do_reload() {
kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;
;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;
;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;
;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;
;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;
;
esacexit 0#centos7+脚本
#! /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# Author:licess
# website:http://lnmp.orgPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAMEset -e
[ -x "$DAEMON" ] || exit 0do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}do_stop() {
kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}do_reload() {
kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;
;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;
;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;
;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;
;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;
;
esacexit 0
#推荐配置nginx.confuserwww www;
worker_processesauto;
#error_loglogs/error.log;
error_log/dev/null ;
#error_loglogs/error.lognotice;
#error_loglogs/error.logerror;
pid /usr/local/nginx/logs/nginx.pid;
#pidlogs/nginx.pid;
worker_rlimit_nofile 65535;
#视情况修改
events {
use epoll;
worker_connections65535;
#连接数,视情况而定
}
http {
#limit_conn相关推荐配置
#limit_conn_log_level error;
#limit_conn_status 503;
#limit_conn_zone $binary_remote_addr zone=one:50m;
# 建立 one储存区 储存每个请求ip的连接数
#limit_conn_zone $server_name zone=two:50m;
# 建立two 储存区域,储存对应域名的建立的连接数量
#limit_req_zone'$binary_remote_addr - $uri' zone=three:50m rate=2r/s;
# 建立three zone储存区域 每个ip 同一个接口uri 最多每秒只能发送两次请求
includemime.types;
default_typeapplication/octet-stream;
#charsetgb2312;
server_names_hash_bucket_size 128;
log_formatmain'$host - $remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/dev/null;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
#keepalive_timeout0;
keepalive_timeout 15;
client_header_timeout 3m;
client_body_timeout 3m;
#send_timeout 3m;
send_timeout 15m;
connection_pool_size 256;
client_header_buffer_size 32k;
large_client_header_buffers 1 128k;
ignore_invalid_headerson;
recursive_error_pageson;
server_name_in_redirect off;
request_pool_size 10m;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 8m;
client_body_buffer_size 256k;
client_body_temp_path /dev/shm/client_body_temp;
proxy_temp_path/usr/local/nginx/proxy_temp;
fastcgi_temp_path/usr/local/nginx/fastcgi_temp;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 8;
gzip_proxied any;
gzip_types text/plain text/css image/jpeg image/gif image/png application/javascript text/xml application/xml application/sml+rss text/javascript application/x-httpd-php;
gzip_min_length 1k;
gzip_buffers 1 64k;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
fastcgi_buffers 8 256k;
#fastcgi_buffer_size 128k;
fastcgi_buffer_size 256k;
fastcgi_busy_buffers_size 512k;
#fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 256k;
fastcgi_temp_file_write_size 512k;
include /usr/local/nginx/conf/vhosts/*.conf;
}#limit_conn模块 视情况而定
#server
# {
# limit_conn one 10;
# one区域 每个ip 连接数量不能超过10个
# limit_conn two 10000;
# two区域 每个server_name处理数量不能超过10000个
# limit_req zone=three burst=5;
# three 区域 同一个接口 每个ip 请求数量1秒内的限制 开启
#}
【NGINX|Nginx编译安装与配置优化】
推荐阅读
- 监控nginx
- 《Unix网络编程》第一卷第三版|《Unix网络编程》第一卷第三版 源码编译
- maven使用tomcat7插件编译jsp出错
- 嵌入式(编译内核、根文件系统等)
- Linux|Linux 服务器nginx相关命令
- Nginx|Nginx Tomcat 构造https服务应对苹果要求
- Centos6源码编译安装2.4版本http服务步骤
- 一键编译部署Mysql
- Nginx详细教程
- Nginx进阶(1)——nginx本地代理配置访问node服务