您的位置:首页 > 运维架构 > Nginx

Nginx常用命令

2016-01-15 11:29 771 查看

Linux下的nginx启动、重新启动

启动

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf


-c制定配置文件的路径,不加-nginx会自动加载默认路径的配置文件。

重启

sudo /usr/nginx/sbin/nginx -s reload


停止

ps -ef |grep nginx
kill -QUIT 进程号
kill -9 进程号


常见问题

1、设置操作系统参数ULIMIT -n

在Nginx优化的过程中,将 connections加大的时候Nginx发出警告worker_connections exceed open file resource limit: 1024此警告的问题是受限于Linux的最大文件数限制。

ulimit -n


查看用户打开的最大文件数:1024。此处的1024是每个进程打开的最大文件数,对于系统的最大限制:

more /proc/sys/fs/file-max
355828


file-max是整机可以打开的fd数目,对确定的进程仍然是1024个。那么我们来修改我们的限制。

修改/etc/security/limits.conf文件,在文件中添加如下行:

* soft noproc 65535
* hard noproc 65535
* soft nofile 65535
* hard nofile 65535


就是限制了任意用户的最大线程数和文件数为65535。

其中为所有用户的打开文件数限制,可用’‘号表示修改所有用户的限制;soft或hard指定要修改软限制还是硬限制;10240则指定了想要修改的新的限制值,即最大打开文件数(请注意软限制值要小于或等于硬限制)。修改完后保存文件。

修改/etc/pam.d/login文件,在文件中添加如下行:

session required /lib/security/pam_limits.so


这是告诉Linux在用户完成系统登录后,应该调用pam_limits.so模块来设置系统对该用户可使用的各种资源数量的最大限制(包括用户可打开的最大文件数限制),而pam_limits.so模块就会从/etc/security/limits.conf文件中读取配置来设置这些限制值。修改完后保存此文件。

修改/etc/rc.local脚本,在脚本中添加如下行:

echo “65535"> /proc/sys/fs/file-max


这是让Linux在启动完成后强行将系统级打开文件数硬限制设置为65535。修改完后保存此文件。

如果比较懒的话可以在 rc.local 里面 明确命令 ulimit -n xxxxx

除了在系统中进行设定 nofile(fs.file-max) 值外,可以在 nginx.conf 中指定worker_process可以使用的nofile值,如:



2、通过nginx下载文件刚开始下载一会就停止了

Nginx报错:open() /usr/local/nginx/proxy-temp/2180/000082 failed Permisssion denied一开始以为是权限问题,后发现是proxy_busy_buffer_size参数的影响,下载文件为21M,此参数为512K,所以下载到几百K就停了,改成10M时下载到10M就停了,说明与这个参数有关。

proxy_busy_buffer_size设置缓冲区大小,从代理后端服务器取得的第一部分的响应内容,会放到这里。

如果response的内容很大,nginx会接收并把他们写入到temp_file里去,大小由proxy_max_temp_file_size控制,如果busy的buffer传输完了,会从temp_file里面接着读数据,直到传输完毕。

解决办法:

1、 把proxy_busy_buffer_size调大。

2、 把下载文件放到nginx托管目录里,不从后端服务器传,让nginx直接管理静态资源。

3、 直接绕过nginx,从后端tomcat下载。

4、 控制下载速度:nginx.conf: limit_rate 150k

Nginx的安装

nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好。ububtu平台可以使用以下指令

apt-get install build-essential

apt-get install libtool

CentOS下使用

yum –y install gcc gcc-c++ kernel-devel

一般我们都需要先装pcre,zlib,前者为了重写rewrite,后者为了gzip压缩。

1、安装pcre库(也可以直接用yum安装,但如果用下面方式安装再后面安装nginx时要指定pcre的安装目录)

yum install pcre-devel #使用yum安装pcre-devel

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /usr/local/src

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz

tar -zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure

make

make install

2、安装zlib库(也可以直接用yum安装,但如果用下面方式安装再后面安装nginx时要指定zlib库的安装目录)http://zlib.net/zlib-1.2.7.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /usr/local/src

wget http://zlib.net/zlib-1.2.7.tar.gz

tar -zxvf zlib-1.2.7.tar.gz

cd zlib-1.2.7

./configure

make

make install

3、安装Nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个。官网下载好ngnix最新版的安装包,源码目录我选/usr/local/src/webware,解压,然后执行:

./configure –prefix=/usr/local/src/webware

(如果上面安装pcre和zlib时是下载的官方源码包,那么这里要指定pce和zlib的安装目录:

./configure --prefix=/usr/local/src/webware --with-pcre=/usr/local/src/pcre-8.21  --with-zlib=/usr/local/src/zlib-1.2.7)
-------------------------------------------------------------------------------------------


附1.编译nginx时报错及解决办法

报错内容:./configure: error: the HTTP rewrite module requires the PCRE library.

解决方法:yum -y install pcre-devel

报错内容:./configure: error: the HTTP cache module requires md5 functions

from OpenSSL library. You can either disable the module by using

–without-http-cache option, or install the OpenSSL library into the system,

or build the OpenSSL library statically from the source with nginx by using

–with-http_ssl_module –with-openssl= options.

解决方法:yum -y install openssl openssl-devel

附2.nginx编译选项

有temp-path字样的编译选项所设置都是nginx在运行时产生的临时文件的路径,pid-path,lock-path也是临时文件路径,log-path是日志文件路径,一般情况下其实这些是不必要的,nginx默认会统统把生成的这些文件放在自己的安装目录下。

Nginx的configure命令支持以下参数:

./configure --help
--prefix=PATH              set installation prefix,默认为 /usr/local/nginx
--sbin-path=PATH           set nginx binary pathname nginx,可执行文件的路径,默认为<prefix>/sbin/nginx
--conf-path=PATH          set nginx.conf pathname,
nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为<prefix>/conf/nginx.conf
--error-log-path=PATH              set error log pathname,如果在nginx.conf中没有指定error log指令,则此参数指定的即为默认的error log路径,默认为<prefix>/logs/error.log
--pid-path=PATH                    set nginx.pid pathname,
如果在nginx.conf中没有指定pid指令,则此参数指定的即为默认的nginx.pid的路径
,默认为 <prefix>/logs/nginx.pid
--lock-path=PATH                   set nginx.lock pathname
--user=USER                        set non-privileged user for worker processes,
在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--group=GROUP                      set non-privileged group for worker processes,
在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--builddir=DIR                     set build directory
--with-rtsig_module                enable rtsig module
--with-select_module               enable select module
--without-select_module            disable select module
开启或禁用SELECT模式,如果 configure没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+),rtsig(实时信号
)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相同,都是采用轮询方法)SELECT模式将是默认安装模式。
--with-poll_module                 enable poll module
--without-poll_module              disable poll module
启用或禁用构建一个模块来允许服务器使用poll()方法。如果平台不支持的kqueue,epoll,rtsig或/dev/poll,该模块将自动建立。
--with-file-aio                      enable file AIO support
--with-ipv6                        enable IPv6 support
--with-http_ssl_module             enable ngx_http_ssl_module 使用https协议模块,使NGINX可以支持HTTPS请求。默认情况下,该模块没有被构建。建立并运行此模块需要安装OpenSSL库,在DEBIAN上是libssl。
--with-http_realip_module          enable ngx_http_realip_module
--with-http_addition_module        enable ngx_http_addition_module
--with-http_xslt_module            enable ngx_http_xslt_module
--with-http_image_filter_module     enable ngx_http_image_filter_module
--with-http_geoip_module          enable ngx_http_geoip_module
--with-http_sub_module            enable ngx_http_sub_module 启用 "server status" 页
--with-http_dav_module            enable ngx_http_dav_module
--with-http_flv_module             enable ngx_http_flv_module
--with-http_mp4_module           enable ngx_http_mp4_module
--with-http_gzip_static_module      enable ngx_http_gzip_static_module
--with-http_random_index_module   enable ngx_http_random_index_module
--with-http_secure_link_module     enable ngx_http_secure_link_module
--with-http_degradation_module     enable ngx_http_degradation_module
--with-http_stub_status_module     enable ngx_http_stub_status_module
下面这些without的参数是编译ng时会自动启用的,所以这里会有这些without的参数供大家选择不启用哪些模块.
--without-http_charset_module      disable ngx_http_charset_module
--without-http_gzip_module        disable ngx_http_gzip_module 若要启用此模块需要zlib库的支持
--without-http_ssi_module          disable ngx_http_ssi_module
--without-http_userid_module       disable ngx_http_userid_module
--without-http_access_module       disable ngx_http_access_module
--without-http_auth_basic_module   disable ngx_http_auth_basic_module
--without-http_autoindex_module    disable ngx_http_autoindex_module
--without-http_geo_module         disable ngx_http_geo_module
--without-http_map_module         disable ngx_http_map_module
--without-http_split_clients_module  disable ngx_http_split_clients_module
--without-http_referer_module      disable ngx_http_referer_module
--without-http_rewrite_module      disable ngx_http_rewrite_module 不编译重写模块,注意若要启用rewrite模块需要pcre库的支持.
--without-http_proxy_module        disable ngx_http_proxy_module
--without-http_fastcgi_module       disable ngx_http_fastcgi_module
--without-http_uwsgi_module        disable ngx_http_uwsgi_module
--without-http_scgi_module         disable ngx_http_scgi_module
--without-http_memcached_module   disable ngx_http_memcached_module
--without-http_limit_conn_module    disable ngx_http_limit_conn_module
--without-http_limit_req_module     disable ngx_http_limit_req_module
--without-http_empty_gif_module    disable ngx_http_empty_gif_module
--without-http_browser_module      disable ngx_http_browser_module
--without-http_upstream_ip_hash_module     disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module disable ngx_http_upstream_least_conn_module
--without-http_upstream_keepalive_module   disable ngx_http_upstream_keepalive_module
--with-http_perl_module            enable ngx_http_perl_module 指定 perl 执行文件的路径
--with-perl_modules_path=PATH      set Perl modules path 指定 perl 模块的路径
--with-perl=PATH                   set perl binary pathname
--http-log-path=PATH               set http access log pathname
在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log
--http-client-body-temp-path=PATH  set path to store  http client request body temporary files
--http-proxy-temp-path=PATH        set path to store  http proxy temporary files
--http-fastcgi-temp-path=PATH       set path to store  http fastcgi temporary files
--http-uwsgi-temp-path=PATH        set path to store  http uwsgi temporary files
--http-scgi-temp-path=PATH          set path to store  http scgi temporary files
--without-http                     disable HTTP server 禁用 HTTP server??????
--without-http-cache               disable HTTP cache
--with-mail    enable POP3/IMAP4/SMTP proxy module 启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module             enable ngx_mail_ssl_module
--without-mail_pop3_module         disable ngx_mail_pop3_module
--without-mail_imap_module         disable ngx_mail_imap_module
--without-mail_smtp_module         disable ngx_mail_smtp_module
--with-google_perftools_module     enable ngx_google_perftools_module
--with-cpp_test_module             enable ngx_cpp_test_module
--add-module=PATH                  enable an external module
--with-cc=PATH                     set C c
c692
ompiler pathname 指定 C 编译器的路径
--with-cpp=PATH                    set C preprocessor pathname 指定 C 预处理器的路径
--with-cc-opt=OPTIONS              set additional C compiler options
Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD,it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors,then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".
将额外的参数添加到CFLAGS变量。例如,当你在FreeBSD上使用PCRE库时需要使用:--with-cc-opt="-I /usr/local/include" 。如需要增加select()支持的文件数量:--with-cc-opt="-D FD_SETSIZE=2048"
--with-ld-opt=OPTIONS       设置附加的参数,将用于在链接期间。例如,当在FreeBSD下使用该系统的PCRE库,应指定:--with-ld-opt="-L /usr/local/lib"
--with-cpu-opt=CPU             为特定的 CPU 编译,有效的值包括:pentium,pentiumpro,pentium3,pentium4,athlon,opteron,amd64,sparc32,sparc64,ppc64
--without-pcre                  disable PCRE library usage
禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在"location" 配置指令中的正则表达式也需要 PCRE。
--with-pcre                     force PCRE library usage
--with-pcre=DIR                 set path to PCRE library sources 设置PCRE库的源码路径
--with-pcre-opt=OPTIONS         set additional build options for PCRE
--with-pcre-jit                  build PCRE with JIT compilation support
编译PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)??????????
--with-md5=DIR               set path to md5 library sources
--with-md5-opt=OPTIONS        set additional build options for md5
--with-md5-asm                use md5 assembler sources
--with-sha1=DIR                set path to sha1 library sources
--with-sha1-opt=OPTIONS        set additional build options for sha1
--with-sha1-asm                use sha1 assembler sources
--with-zlib=DIR                 set path to zlib library sources 设置zlib库的源码路径
--with-zlib-opt=OPTIONS         set additional build options for zlib
--with-zlib-asm=CPU            use zlib assembler sources optimized
for the specified CPU, valid values: pentium, pentiumpro
--with-libatomic                 force libatomic_ops library usage
--with-libatomic=DIR            set path to libatomic_ops library sources
--with-openssl=DIR             set path to OpenSSL library sources 设置openssl 源码路径
--with-openssl-opt=OPTIONS     set additional build options for OpenSSL
--with-debug                  enable debug logging
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx linux