linux 版怎么配置nginx linux下怎样配置nginx

linux\u4e0b\u5982\u4f55\u914d\u7f6enginx\u670d\u52a1\u5668

\u767e\u5ea6nginx\uff0c\u5c31\u6709\u5f88\u591a\u914d\u7f6e\u5b89\u88c5\u6280\u5de7\u4e86\u3002\u95ee\u9898\u7684\u6d89\u53ca\u8303\u56f4\u592a\u5e7f\u4e86\uff0c\u4e0d\u597d\u56de\u7b54\u3002
\u4ec5\u4f5c\u53c2\u8003\uff1a
#\u8fd0\u884c\u7528\u6237user nobody;#\u542f\u52a8\u8fdb\u7a0b,\u901a\u5e38\u8bbe\u7f6e\u6210\u548ccpu\u7684\u6570\u91cf\u76f8\u7b49worker_processes 1;#\u5168\u5c40\u9519\u8bef\u65e5\u5fd7\u53caPID\u6587\u4ef6#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;#\u5de5\u4f5c\u6a21\u5f0f\u53ca\u8fde\u63a5\u6570\u4e0a\u9650events { #epoll\u662f\u591a\u8def\u590d\u7528IO(I/O Multiplexing)\u4e2d\u7684\u4e00\u79cd\u65b9\u5f0f, #\u4ec5\u7528\u4e8elinux2.6\u4ee5\u4e0a\u5185\u6838,\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8nginx\u7684\u6027\u80fd use epoll; #\u5355\u4e2a\u540e\u53f0worker process\u8fdb\u7a0b\u7684\u6700\u5927\u5e76\u53d1\u94fe\u63a5\u6570 worker_connections 1024; # \u5e76\u53d1\u603b\u6570\u662f worker_processes \u548c worker_connections \u7684\u4e58\u79ef # \u5373 max_clients = worker_processes * worker_connections # \u5728\u8bbe\u7f6e\u4e86\u53cd\u5411\u4ee3\u7406\u7684\u60c5\u51b5\u4e0b\uff0cmax_clients = worker_processes * worker_connections / 4 \u4e3a\u4ec0\u4e48 # \u4e3a\u4ec0\u4e48\u4e0a\u9762\u53cd\u5411\u4ee3\u7406\u8981\u9664\u4ee54\uff0c\u5e94\u8be5\u8bf4\u662f\u4e00\u4e2a\u7ecf\u9a8c\u503c # \u6839\u636e\u4ee5\u4e0a\u6761\u4ef6\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u7684Nginx Server\u53ef\u4ee5\u5e94\u4ed8\u7684\u6700\u5927\u8fde\u63a5\u6570\u4e3a\uff1a4 * 8000 = 32000 # worker_connections \u503c\u7684\u8bbe\u7f6e\u8ddf\u7269\u7406\u5185\u5b58\u5927\u5c0f\u6709\u5173 # \u56e0\u4e3a\u5e76\u53d1\u53d7IO\u7ea6\u675f\uff0cmax_clients\u7684\u503c\u987b\u5c0f\u4e8e\u7cfb\u7edf\u53ef\u4ee5\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570 # \u800c\u7cfb\u7edf\u53ef\u4ee5\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u548c\u5185\u5b58\u5927\u5c0f\u6210\u6b63\u6bd4\uff0c\u4e00\u822c1GB\u5185\u5b58\u7684\u673a\u5668\u4e0a\u53ef\u4ee5\u6253\u5f00\u7684\u6587\u4ef6\u6570\u5927\u7ea6\u662f10\u4e07\u5de6\u53f3 # \u6211\u4eec\u6765\u770b\u770b360M\u5185\u5b58\u7684VPS\u53ef\u4ee5\u6253\u5f00\u7684\u6587\u4ef6\u53e5\u67c4\u6570\u662f\u591a\u5c11\uff1a # $ cat /proc/sys/fs/file-max # \u8f93\u51fa 34336 # 32000 < 34336\uff0c\u5373\u5e76\u53d1\u8fde\u63a5\u603b\u6570\u5c0f\u4e8e\u7cfb\u7edf\u53ef\u4ee5\u6253\u5f00\u7684\u6587\u4ef6\u53e5\u67c4\u603b\u6570\uff0c\u8fd9\u6837\u5c31\u5728\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u627f\u53d7\u7684\u8303\u56f4\u4e4b\u5185 # \u6240\u4ee5\uff0cworker_connections \u7684\u503c\u9700\u6839\u636e worker_processes \u8fdb\u7a0b\u6570\u76ee\u548c\u7cfb\u7edf\u53ef\u4ee5\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u603b\u6570\u8fdb\u884c\u9002\u5f53\u5730\u8fdb\u884c\u8bbe\u7f6e # \u4f7f\u5f97\u5e76\u53d1\u603b\u6570\u5c0f\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u76ee # \u5176\u5b9e\u8d28\u4e5f\u5c31\u662f\u6839\u636e\u4e3b\u673a\u7684\u7269\u7406CPU\u548c\u5185\u5b58\u8fdb\u884c\u914d\u7f6e # \u5f53\u7136\uff0c\u7406\u8bba\u4e0a\u7684\u5e76\u53d1\u603b\u6570\u53ef\u80fd\u4f1a\u548c\u5b9e\u9645\u6709\u6240\u504f\u5dee\uff0c\u56e0\u4e3a\u4e3b\u673a\u8fd8\u6709\u5176\u4ed6\u7684\u5de5\u4f5c\u8fdb\u7a0b\u9700\u8981\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\u3002 # ulimit -SHn 65535}http { #\u8bbe\u5b9amime\u7c7b\u578b,\u7c7b\u578b\u7531mime.type\u6587\u4ef6\u5b9a\u4e49 include mime.types; default_type application/octet-stream; #\u8bbe\u5b9a\u65e5\u5fd7\u683c\u5f0f 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 \u6307\u4ee4\u6307\u5b9a nginx \u662f\u5426\u8c03\u7528 sendfile \u51fd\u6570\uff08zero copy \u65b9\u5f0f\uff09\u6765\u8f93\u51fa\u6587\u4ef6\uff0c #\u5bf9\u4e8e\u666e\u901a\u5e94\u7528\uff0c\u5fc5\u987b\u8bbe\u4e3a on, #\u5982\u679c\u7528\u6765\u8fdb\u884c\u4e0b\u8f7d\u7b49\u5e94\u7528\u78c1\u76d8IO\u91cd\u8d1f\u8f7d\u5e94\u7528\uff0c\u53ef\u8bbe\u7f6e\u4e3a off\uff0c #\u4ee5\u5e73\u8861\u78c1\u76d8\u4e0e\u7f51\u7edcI/O\u5904\u7406\u901f\u5ea6\uff0c\u964d\u4f4e\u7cfb\u7edf\u7684uptime. sendfile on; #tcp_nopush on; #\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #\u5f00\u542fgzip\u538b\u7f29 gzip on; gzip_disable "MSIE [1-6]."; #\u8bbe\u5b9a\u8bf7\u6c42\u7f13\u51b2 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #\u8bbe\u5b9a\u865a\u62df\u4e3b\u673a\u914d\u7f6e server { #\u4fa6\u542c80\u7aef\u53e3 listen 80; #\u5b9a\u4e49\u4f7f\u7528 www.zz04.com\u8bbf\u95ee server_name www.zz04.com; #\u5b9a\u4e49\u670d\u52a1\u5668\u7684\u9ed8\u8ba4\u7f51\u7ad9\u6839\u76ee\u5f55\u4f4d\u7f6e root html; #\u8bbe\u5b9a\u672c\u865a\u62df\u4e3b\u673a\u7684\u8bbf\u95ee\u65e5\u5fd7 access_log logs/nginx.access.log main; #\u9ed8\u8ba4\u8bf7\u6c42 location / { #\u5b9a\u4e49\u9996\u9875\u7d22\u5f15\u6587\u4ef6\u7684\u540d\u79f0 index index.php index.html index.htm; } # \u5b9a\u4e49\u9519\u8bef\u63d0\u793a\u9875\u9762 error_page 500 502 503 504 /50x.html; location = /50x.html { } #\u9759\u6001\u6587\u4ef6\uff0cnginx\u81ea\u5df1\u5904\u7406 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #\u8fc7\u671f30\u5929\uff0c\u9759\u6001\u6587\u4ef6\u4e0d\u600e\u4e48\u66f4\u65b0\uff0c\u8fc7\u671f\u53ef\u4ee5\u8bbe\u5927\u4e00\u70b9\uff0c #\u5982\u679c\u9891\u7e41\u66f4\u65b0\uff0c\u5219\u53ef\u4ee5\u8bbe\u7f6e\u5f97\u5c0f\u4e00\u70b9\u3002 expires 30d; } #PHP \u811a\u672c\u8bf7\u6c42\u5168\u90e8\u8f6c\u53d1\u5230 FastCGI\u5904\u7406. \u4f7f\u7528FastCGI\u9ed8\u8ba4\u914d\u7f6e. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #\u7981\u6b62\u8bbf\u95ee .htxxx \u6587\u4ef6 location ~ /.ht { deny all; } }}

\u4f60\u8981\u8bbe\u5b9a\u4e00\u4e0b\u8bbf\u95ee\u540e\u7aef\u670d\u52a1\u5668\u7684\u65b9\u5f0f\uff0c\u662f\u8f6e\u8be2\u8fd8\u662fip hash\u7b49\uff0c\u8bbe\u7f6e\u4e86\u624d\u53ef\u4ee5\u5207\u6362 server 110.41.90.143 weight=5 max_fails=3 fail_timeout=10s; server 110.41.90.138 weight=5 max_fails=3 fail_timeout=10s; \u7c7b\u4f3c\u6211\u5199\u7684\u8fd9\u6837\uff0c\u660e\u767d\u4e86\u5427

配置Nginx服务

Nginx是一款相当优秀的用于部署动态网站的服务程序,Nginx最初是为俄罗斯门户站点而设计的网站服务软件,作为一款轻量级的网站服务软件,因其稳定性和丰富的功能而深受信赖,但最最最被认可的是低系统资源、占用内存少且并发能力强,目前国内如新浪、网易、腾讯等门户站均在使用,市场占有份额一直保持在15-16%左右(2015年最新数据)。

Nginx程序的稳定性来自于它采用了分阶段的资源分配技术,使得CPU与内存占用率会非常低,所以使用Nginx程序部署动态网站环境不仅十分的稳定、高效,而且消耗更少的系统资源,丰富的模块功能也几乎与Apache程序数量相同,现在已经完全的支持了proxy、rewrite、mod_fcgi、ssl、vhosts等常用模块。而且还支持了热部署技术,即能够可以7*24不间断提供服务,即便运行数月也无须重启,而且还可以在不暂停服务的情况下直接对Nginx服务程序进行升级。

坦白来讲,虽然Nginx程序的代码质量非常高,代码很规范,技术成熟,模块扩展也很容易,但Nginx依然存在不少问题,比如Nginx是由俄罗斯人创建的,所以在资料文档方面还并不完善,中文教材的质量更是鱼龙混杂,但Nginx近年来增长势头迅猛,预测未来应该能够在轻量级HTTP服务器市场有不错的未来。

安装PCRE(Perl兼容的正则表达式库,解压与编译过程已省略):
[root@linuxprobe ~]# cd /usr/local/src
[root@linuxprobe src]# mkdir /usr/local/pcre
[root@linuxprobe src]# tar xzvf pcre-8.35.tar.gz
[root@linuxprobe src]# cd pcre-8.35
[root@linuxprobe pcre-8.35]# ./configure --prefix=/usr/local/pcre
[root@linuxprobe pcre-8.35]# make
[root@linuxprobe pcre-8.35]# make install

安装openssl服务程序(解压与编译过程已省略):
[root@linuxprobe pcre-8.35]# cd /usr/local/src
[root@linuxprobe src]# mkdir /usr/local/openssl
[root@linuxprobe src]# tar xzvf openssl-1.0.1h.tar.gz
[root@linuxprobe src]# cd openssl-1.0.1h
[root@linuxprobe openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
[root@linuxprobe openssl-1.0.1h]# make
[root@linuxprobe openssl-1.0.1h]# make install

把openssl服务程序命令目录添加到环境变量中(永久生效):
[root@linuxprobe pcre-8.35]# vim /etc/profile
//将配置文件最下面的参数追加参数为:
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
[root@linuxprobe pcre-8.35]# source /etc/profile

安装zlib数据压缩函数库(解压与编译过程已省略):
[root@linuxprobe pcre-8.35]# cd /usr/local/src
[root@linuxprobe src]# mkdir /usr/local/zlib
[root@linuxprobe src]# tar xzvf zlib-1.2.8.tar.gz
[root@linuxprobe src]# cd zlib-1.2.8
[root@linuxprobe zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
[root@linuxprobe zlib-1.2.8]# make
[root@linuxprobe zlib-1.2.8]# make install

创建用于执行nginx服务的用户:
[root@linuxprobe zlib-1.2.8]# cd ..
[root@linuxprobe src]# useradd www -s /sbin/nologin

安装nginx服务程序(openssl,zlib,pcre要写成源码解压路径!!!):
[root@linuxprobe src]# tar xzvf nginx-1.6.0.tar.gz
[root@linuxprobe src]# cd nginx-1.6.0/
[root@linuxprobe nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35
[root@linuxprobe nginx-1.6.0]# make
[root@linuxprobe nginx-1.6.0]# make install

创建nginx程序脚本(将下面的参数直接复制进去即可):
[root@linuxprobe nginx-1.6.0]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@linuxprobe nginx-1.6.0]# chmod 755 /etc/rc.d/init.d/nginx

重启nginx服务程序并添加到开机启动项:
[root@linuxprobe nginx-1.6.0]# /etc/rc.d/init.d/nginx restart
Restarting nginx (via systemctl): [ OK ]
[root@linuxprobe nginx-1.6.0]# chkconfig nginx on

此时可以通过访问IP来判断nginx服务是否顺利运行

LNMP(即Linux+Nginx+MYSQL+PHP)是目前非常热门的动态网站部署架构,如果你是在搭建这种部署的话可以看下http://www.linuxprobe.com/chapter-20.html

扩展阅读:linux nfs配置 ... linux snmp配置详解 ... linux如何配置yum源 ... linux scp命令用法 ... linux bond配置查询 ... linux chmod ... linux 配置永久路由 ... linux yum删除命令 ... linux samba服务器的配置 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网