Nginx編譯安裝與配置優化

#安裝指令 基於centos

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  
groupadd www
useradd www -g www
wget  https://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.org

PATH=/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/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_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
 ;;
esac

exit 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.org

PATH=/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/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_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
 ;;
esac

exit 0

 

 

#推薦配置nginx.conf

user  www www;
worker_processes  auto;
 
#error_log  logs/error.log;
error_log   /dev/null ;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  error;
pid /usr/local/nginx/logs/nginx.pid;
#pid        logs/nginx.pid;
worker_rlimit_nofile 65535;#視情況修改
 
events {
    use epoll;
    worker_connections  65535;#連接數,視情況而定
}
 
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 最多每秒只能發送兩次請求
    include       mime.types;
    default_type  application/octet-stream;
#    charset     gb2312;
    server_names_hash_bucket_size 128;
    log_format  main  '$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;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;
    #keepalive_timeout  0;
    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_headers   on;
    recursive_error_pages    on;
    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秒內的限制 開啓
#}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章