1. 413 Request Entity Too Large
原因:报错是http request请求body太大,被nginx拒绝 解决方法:在配置文件里面修改client_max_body_size 10M 增大一些
2. HTTP/1.1 405 Method not allowed
原因:这个是由于客户端在用post的方式去请求了静态资源,而静态资源nginx不止允许通过get方式去请求 解决方法:修改nginx的源码,重新编译即可 找到文件http/modules/ngx_http_static_module.c 注释下面的禁止代码即可
/*
if (r->method & NGX_HTTP_POST) {
return NGX_HTTP_NOT_ALLOWED;
}
*/
12345
3.post内容在lua里面无法获取body内容
检查client_max_body_size和client_body_buffer_size是否设置过小 另外,在读取的时候server中使用lua_need_request_body on; 或者在location lua代码块中使用 ngx.req.read_body()
4:nginx错误日志无法关闭
设置error_log off;后任然会输出文件名为off的错误日志,根本原因是错误日志不能off,只能error_log /dev/null;
5.openresty安装geoip时错误
./configure: error: the GeoIP module requires the GeoIP library apt-get install libgeoip-dev
6.域名太长
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32 解决:server_names_hash_bucket_size 64;
7.nginx post json 跨域问题
Cross-Origin Read Blocking (CORB) blocked cross-origin with MIME type application/json 解决方式为:
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}
123456789
8.nginx: [error] invalid PID number问题处理
在启动Nginx服务时,无法正常启动,报错误信息如下
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
1
解决方法:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1
使用nginx -c的参数指定nginx.conf文件的位置。
然后再重新启动,解决问题,可以通过ps -ef|grep nginx 看到服务已经启动成功。
9.检查nginx 是否配置成功方法
10.Nginx其他命令
Nginx 服务器重启命令,关闭
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
123
关闭Nginx:
nginx -s stop :快速停止nginx
quit :完整有序的停止nginx
123
其他的停止Nginx 方式:
ps -ef | grep nginx
kill -QUIT 主进程号 :从容停止Nginx
kill -TERM 主进程号 :快速停止Nginx
pkill -9 nginx :强制停止Nginx
1234567
平滑重启Nginx:
kill -HUP 主进程号
1
11.Nginx响应超时 upstream timed out 问题处理
环境介绍
服务器:centos7.2
应用:tomcat集群
服务:nginx 代理
问题描述:
这段时间,听项目组项目经理和业务需求人员跟我反馈,线上业务人员在操作业务交易时,有时会出现nginx错误信息,一开始以为是客户人员自己误操作,因为频度不是很高半年多就反馈错一两次,只是今天刚好测试人员在操作时,也出现nginx错误信息,如下图: 还好我们测试环境也是模拟生产环境搭建集群部署,那如果测试环境出现同样问题,那说明确实存在问题,应该是配置问题,看到我们测试人员模拟出的问题,我看了后台日志:
2018/11/08 15:58:49 [error] 20952#0: *285940 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.100.81.89, server: localhost, request: "POST /MylikeOMS/a/cust/activityPlanVisitPlan/save HTTP/1.1", upstream:
1
从日志可以看出nginx代理配置时,Connection timed out设置出问题,于是修改了,nginx.conf 在server {里设置如下
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
123
然后重启nginx即可:
然后重新加载新的配置/sbin/nginx -s reload
这时再让我们测试人员再重新复测,问题解决。
Nginx使用报错
1. ./configure: error: the HTTP rewrite module requires the PCRE library
解决: 安装pcre-devel解决问题 yum -y install pcre-devel
2.413 Request Entity Too Large
在上传时Nginx返回了413错误,这一般就是上传文件大小超过Nginx配置引起。
解决: 在Nginx.conf配置文件增加client_max_body_size的设置,这个值默认是1M,可以增加到8M以提高文件大小限制
3.nginx:[emerg] unknown directive
出现这种情况一般就是缺少了模块。 解决: 比如出现nginx:[emerg]unknown directive ssl,那么在安装时添加 SSL 模块./configure –with-http_ssl_module进入安装成功的 sbin 目录下,执行以下命令可以看到 SSL 模块。
[root@nginx sbin]# ./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.1.0e-fips 16 Feb 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module
123456
4.打开nginx.conf文件提示“.nginx.conf.swp”存在
原因:在用vim打开一个文件时,其会产生一个cmd.swap文件,用于保存数据,当文件非正常关闭时,可用此文件来恢复,当正常关闭时,此文件会被删除,非正常关闭时,不会被删除,所以提示存在.swap文件
解决: 1)ls -a 查看隐藏文件; 2)执行rm -rf .nginx.conf.swp命令即可。
Nginx出现403 forbidden 报错的解决方法
1、由于启动用户和nginx工作用户不一致所致
1.1查看nginx的启动用户,发现是nobody,而为是用root启动的
命令:ps aux | grep "nginx: worker process" | awk'{print $1}'
1.2将nginx.config的user改为和启动用户一致,
命令:vi conf/nginx.conf
2、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件。
1. server {
2. listen 80;
3. server_name localhost;
4. index index.php index.html;
5. root /data/www/;
6. }
如果在/data/www/下面没有index.php,index.html的时候,直接文件,会报403 forbidden。
3、权限问题,如果nginx没有web目录的操作权限,也会出现403错误。
解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决
1. chmod -R 777 /data
2. chmod -R 777 /data/www/
4、selinux为开启状态
4.1、查看当前selinux的状态。
getenforce
4.2、将SELINUX=enforcing 修改为 SELINUX=disabled 状态。
1. vi /etc/selinux/config
2.
3. #SELINUX=enforcing
4. SELINUX=disabled
4.3、重启生效。reboot。
1. reboot
nginx配置upstream使用带有下划线的名称转发到tomcat发生400错误
今天部署nacos配置nginx的时候,怎么弄都是400。找了好多资料才解决。 原来是upstream后面名称不能有下划线,但是我的其他服务器上的nginx带有下划线没有问题。有可能是系统配置的关系吧。 我把它改成减号,重新reload一下,再访问就可以了0.0. https://www.jianshu.com/p/e489be2907cd
Nginx常见异常
"upstream prematurely(过早的) closed connection"
1
请求uri的时候出现的异常,是由于upstream还未返回应答给用户时用户断掉连接造成的,对系统没有影响,可以忽略
"recv() failed (104: Connection reset by peer)"
1
(1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉;
(2)客户关掉了浏览器,而服务器还在给客户端发送数据;
(3)浏览器端按了Stop
"(111: Connection refused) while connecting to upstream"
1
用户在连接时,若遇到后端upstream挂掉或者不通,会收到该错误
"(111: Connection refused) while reading response header from upstream"
1
用户在连接成功后读取数据时,若遇到后端upstream挂掉或者不通,会收到该错误
"(111: Connection refused) while sending request to upstream"
1
Nginx和upstream连接成功后发送数据时,若遇到后端upstream挂掉或者不通,会收到该错误
"(110: Connection timed out) while connecting to upstream"
1
nginx连接后面的upstream时超时
"(110: Connection timed out) while reading upstream"
1
nginx读取来自upstream的响应时超时
"(110: Connection timed out) while reading response header from upstream"
1
nginx读取来自upstream的响应头时超时
"(110: Connection timed out) while reading upstream"
1
nginx读取来自upstream的响应时超时
"(104: Connection reset by peer) while connecting to upstream"
1
upstream发送了RST,将连接重置
"upstream sent invalid header while reading response header from upstream"
1
upstream发送的响应头无效
"upstream sent no valid HTTP/1.0 header while reading response header from upstream"
1
upstream发送的响应头无效
"client intended to send too large body"
1
用于设置允许接受的客户端请求内容的最大值,默认值是1M,client发送的body超过了设置值
"reopening logs"
1
用户发送kill -USR1命令
"gracefully shutting down"
1
用户发送kill -WINCH命令
"no servers are inside upstream"
1
upstream下未配置server
"no live upstreams while connecting to upstream"
1
upstream下的server全都挂了
"SSL_do_handshake() failed"
1
SSL握手失败
"ngx_slab_alloc() failed: no memory in SSL session shared cache"
1
ssl_session_cache大小不够等原因造成
"could not add new SSL session to the session cache while SSL handshaking"
1
启动报错nginx: [emerg] unknown directive “ssl”
在Ubuntu安装配置Nginx后,执行/etc/init.d/nginx start时,回显报错如下:
nginx: [emerg] unknown directive “ssl” in /usr/local/nginx/conf/nginx.conf:19”
1
这个报错说明没有将ssl模块编译进nginx,需要在configure的时候加上“–with-http_ssl_module”,命令大致如下:
# ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module –with-http_ssl_module
1
然后重新执行make编译后,问题即可解决
Nginx提示CORS :No ‘Access-Control-Allow-Origin’ header 解决办法
有时候你的业务需求,可能会出现跨域的问题,我的博客也是一样的(因为我博客用的站群模式)。当你在我的博客注册的时候,Console控制台会出现如下错误:
Access to Font at 'https://www.fujieace.com/wp-content/themes/fujie/css/fonts/fontawesome-webfont.woff?v=4.4.0' from origin 'https://fujieace.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fujieace.com' is therefore not allowed access. wp-signup.php:1 Access to Font at 'https://www.fujieace.com/wp-content/themes/fujie/css/fonts/fontawesome-webfont.ttf?v=4.4.0' from origin 'https://fujieace.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fujieace.com' is therefore not allowed access. wp-signup.php:1 Access to Font at 'https://www.fujieace.com/wp-content/plugins/comments-like-dislike/fonts/fontawesome-webfont.woff2?v=4.6.3' from origin 'https://fujieace.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fujieace.com' is therefore not allowed access. wp-signup.php:1 Access to Font at 'https://www.fujieace.com/wp-content/plugins/comments-like-dislike/fonts/fontawesome-webfont.woff?v=4.6.3' from origin 'https://fujieace.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fujieace.com' is therefore not allowed access. wp-signup.php:1 Access to Font at 'https://www.fujieace.com/wp-content/plugins/comments-like-dislike/fonts/fontawesome-webfont.ttf?v=4.6.3' from origin 'https://fujieace.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fujieace.com' is therefore not allowed access. 12345
上面报错大致意思如下:
从原产地’https://fujieace.com’访问’https://www.fujieace.com/wp-content/themes/fujie/css/fonts/fontawesome-webfont.woff?v=4.4.0’中的字体 已被CORS策略阻止:请求的资源上没有“Access-Control-Allow-Origin”标头。 因此,不允许来源“https://fujieace.com”访问。
解决办法 由于我博客被CORS策略阻止的只有字体,因此我只需要nginx配置字体跨域就行了。就不用配置其它跨域了。毕竟:Access-Control-Allow-Origin * 跨域是很危险的。
注意:nginx.conf配置好了,一定要重启nginx。
nginx中Access-Control-Allow-Origin字体跨域配置 方法:
location ~* \.(eot|ttf|woff|woff2|svg)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}
12345
以上nginx.conf这样就可以实现GET,POST,OPTIONS的跨域请求的支持,也可以 add_header Access-Control-Allow-Origin –指定允许的url;
nginx中Access-Control-Allow-Origin 其它跨域配置 示例:
# # 用于nginx的开放式CORS配置 # location / { if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # 自定义标题和标题各种浏览器*应该*可以,但不是 # add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; # # 告诉客户这个飞行前信息有效期为20天 # add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } } 1234567891011121314151617181920212223242526272829303132
nginx: [emerg] bind() to 0.0.0.0:80 failed 解决办法
Nginx是一款轻量级的Web服务器,特点是占有内存少,并发能力强,因而使用比较广泛,今天在一个VPS上重启Nginx时提示错误:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
1
中文翻译
nginx:[emerg] bind()到0.0.0.0:80失败(98:地址已经在使用中)
1
解决办法 那么:如何解决Nginx重启时提示“nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)”错误?
从上图中的报错,可以很清楚的得到这样一个原因,此错误指的是80端口被程序占用了。那么解决方法就简单了,强制关闭端口的占用就可以了,一个简单的命令就解决了。
sudo fuser -k 80/tcp #关闭占用80端口的程序
使用效果:
[root@198.162.1.122 ~]# sudo fuser -k 80/tcp
80/tcp: 2010 2019 2020 2021 2022 2023 2024 2028 2029
12
nginx: [emerg] bind() to 0.0.0.0:443 failed(98:Address already in use)解决方法
今天一位网友让我帮忙看一下他网站关于HTTPS的问题,总之一句话:网站以HTTPS的方式访问不好使了。我的这位网友百分之百的保证他的SSL证书配置以及证书都是没有任何问题的,当我去宝塔启动nginx的时候,却出现了如下错误:
nginx: [emerg] bind() to 0.0.0.0:443 failed(98:Address already in use)
1
中文翻译
nginx:[emerg] bind()到0.0.0.0:443失败(98:地址已经在使用中)
1
解决方法 解决问题必须要知道原因,其实这个报错原因也很简单,就是443端口被占用了。解决起来就更加的简单了,具体操作如下:
1、查看443端口是否被占用?是哪个占用的?
[root@104 ~]# netstat -anon | grep 443
1
2、杀掉占用的443端口的进程
[root@104 ~]# fuser -k 443/tcp
1
3、现在我再去重启Nginx,已经可以正常启动了,HTTPS打开也恢复正常了。
nginx:[emerg] unkown directive “ssl” 和 the “ssl” parameter requires ngx_http_ssl_module 解决办法
今天在用nginx配置https时提示错误
nginx:[emerg] unkown directive "ssl" in /usr/local/nginx/conf/nginx.conf:101;
nginx:[emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:99
12
上述错误两行意思:
1、未知指令”ssl”;
2、“ssl”参数需要ngx_http_ssl_module这个模块;
报错原因: 如果Nginx未开启SSL模块,配置Https时提示错误:
nginx:[emerg] unkown directive "ssl" in /usr/local/nginx/conf/nginx.conf:101;
nginx:[emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:99
原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上–with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经安装过了,怎么添加模块?其实也很简单,往下看:
说明:
我的nginx的安装目录是/usr/local/nginx这个目录,我的源码包在/usr/local/src/nginx-1.11.3目录;
Nginx开启SSL模块“http_ssl_module ”方法
1、切换到nginx源码包:
cd /usr/local/src/nginx-1.11.3
2、查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V
从图中可以看到在configure arguments:后面显示的原有的configure参数如下:
--prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx
3、那么我们的新配置信息就应该这样写:
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --with-http_ssl_module
运行上面的命令即可,等配置完!
4、配置完成后,运行命令
make
这里不要进行make install,否则就是覆盖安装!如果你第一次安装就无所谓了!
5、然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
6、然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
cp ./objs/nginx /usr/local/nginx/sbin/
7、然后启动nginx,仍可以通过命令查看是否已经加入成功?
/usr/local/nginx/sbin/nginx -V
nginx启动报错 :./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object fi
jnydjky@jnydjky:/usr/local/nginx/sbin$ ./nginx
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
找不到pcre库,这个库在 /usr/local/lib 目录,执行: 出现这个问题可能有以下原因:
没有安装 pcre 库 nginx 编译安装时没有指定正确的pcre 地址 pcre 库没有加入到 LD_LIBRARY_PATH 我自己是第二种原因,所以重新编译安装nginx并指定pcre:
./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.38
make
sudo make install
123
nginx Too many open files
在使用Nginx做转发时遇到了连接不上的情况,查看error日志发现一堆的Too many open files报错
2020/06/10 12:47:49 [crit] 21453#21453: *77340 open() "/usr/share/nginx/html/50x.html" failed (24: Too many open files), client: 58.244.114.249, server: _, request: "POST /rest/user/version/ HTTP/1.1", upstream: "http://localhost:6088/rest/version/", host: "xxx.cn"
1
一、文件的限制 首先想到的是linux中的文件限制,因为nginx配置中用的是user nginx,所以需要切换到nginx账号下查看限制
-
切换账号
su - nginx
1
如果出现This account is currently not available错误,是nginx账号不允许登录,使用usermod -s /bin/bash nginx设置一下就行了,详见This account is currently not available
-
查看文件限制
ulimit -Hn # 硬
ulimit -Sn # 软
12
如果出现This account is currently not available错误,是nginx账号不允许登录,使用usermod -s /bin/bash nginx设置一下就行了,详见This account is currently not available
-
查看文件限制
ulimit -Hn # 硬
ulimit -Sn # 软
12
结果发现ulimit值都是65535,感觉问题不在这。 如果ulimit值不够大时,可以用修改/etc/security/limits.conf配置文件,修改或添加
# 配置nginx用户文件限制
nginx soft nofile 65535
nginx hard nofile 65535
# 配置所有用户文件限制
* soft nofile 65535
* hard nofile 65535
1234567
这里需要重启nginx才会生效
二、Nginx配置限制 排除了系统的文件限制,可能就是Nginx自身配置导致的了。
# vim /etc/nginx/nginx.conf 这里为nginx配置文件
worker_rlimit_nofile 65535; //添加
events {
worker_connections 20480; //修改
}
1234567
重启nginx或reload重新加载配置就可以了。
Nginx反向代理413错误
Nginx使用反向代理后,上传文件的时候,出现了413错误。 这是由于上传文件过大引起的,是因为请求长度超过了nginx默认的缓存大小和最大客户端最大请求大小。 在解决办法是在nginx.conf文件中,添加http{}中添加(或修改)client_max_body_size值
http {
...
client_max_body_size 1G
...
}
12345
This account is currently not available
在linux中使用su切换到nginx账号的时候出现了This account is currently not available的错误提示。
使用grep nginx /etc/passwd查看用户权限
$ grep nginx /etc/passwd
nginx:x:997:995:nginx user:/var/cache/nginx:/sbin/nologin
12
这里可以看出,nginx是/sbin/nologin禁止登录的。只要修改这个模式就可以了
$ usermod -s /bin/bash nginx
$ grep nginx /etc/passwd
nginx:x:997:995:nginx user:/var/cache/nginx:/bin/bash
12345
然后就可以用su – nginx切换了
恢复的话改为/sbin/nologin即可
$ usermod -s /sbin/nologin nginx
1
nginx: [emerg] bind() to 0.0.0.0:xxxx failed (13: Permission denied)
在CentOS7环境中安装了nginx后,启动的时候报错nginx: [emerg] bind() to 0.0.0.0:6080 failed (13: Permission denied)。从字面意思上来说,这是权限不足。但出现这种错误可能有多种原因导致的。
1. 端口小于1024时 当端口小于1024时,且运行的账号不是root就会报这个错误。 解决办法是修改nginx的配置文件,以user root;启动
2. 端口大于1024时 检查一下selinux是否开启了。如果开启了关闭selinux试试
setenforce 0
1
如果关了selinux能正常说明端口可能与selinux的端口冲突了或http_port_t中没有开放对应的端口
[root@ptr ~]# semanage port -l | grep 6080
geneve_port_t tcp 6080
12
发现6080是geneve_port_t 的端口,要么修改自己的端口,要么修改geneve_port_t 的端口,然后再把6080添加到http_port_t。附senamage 端口操作命令
senamage 端口 删除 类型 [] 协议 TCP/UDP 端口
semanage port -d -t geneve_port_t -p tcp 6081
senamage 端口 增加 类型 [] 协议 TCP/UDP 端口
semanage port -a -t http_port_t -p tcp 6080
senamage 端口 修改 类型 [] 协议 TCP/UDP 端口
semanage port -m -t geneve_port_t -p tcp 6081
12345678
CentOS7下启动nginx失败Failed to start nginx.service: Unit not found.
操作系统:CentOS7
nginx版本:1.18
1)使用 service nginx start 启动失败 2)解决方法:在/etc/init.d/目录下创建nginx文件将以下代码拷贝进去(网上的代码)
vi /etc/init.d/nginx
1
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
3)设置权限
chomd 755 /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
123
4)启动
service nginx start
1
nginx 10054: An existing connection was forcibly closed by the remote host报错解决方法
在使用nginx过程中,有些通过nginx反向代理转发到tomcat的接口请求莫名被强制取消,以至于无法返回数据,其他接口回数据也很慢,出现这种情况的原因应该是nginx的连接数和tomcat的连接数没有配置好导致。按照下面的方法解决了。 nginx监听接口为80,后台tomcat为82。
1.修改tomcat的server.xml配置。配置信息如下:
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="2048"
maxHttpHeaderSize="8192"
minSpareThreads="512"
maxSpareThreads="1024"
maxIdleTime="30000"/>
<Connector executor="tomcatThreadPool"
port="82"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
acceptCount="1024"
enableLookups="false"
URIEncoding="utf-8"
compression="on"/>
12345678910111213141516
2.修改nginx的nginx.conf文件,配置信息如下: http{}中添加:
keepalive_requests 8192;
keepalive_timeout 180s 180s;
12
server里的location中添加:proxy_http_version 1.1;
server{
location /api/ {
proxy_pass http://172.16.4.120:8580/;
proxy_http_version 1.1;
}
location /gtmcApp {
alias gtmcApp;
index index.html index.htm;
}
location /gtmcApp/api/ {
proxy_pass http://172.16.4.120:8580/;
proxy_http_version 1.1;
}
}
12345678910111213141516171819202122232425
如此,重启tomcat和nginx ,问题解决了。
Nginx配置SSL报错 nginx: [emerg] unknown directive “ssl”
出现如图所示错误,处理办法如下
1.去nginx解压目录下执行
./configure --with-http_ssl_module
1
2.如果报错 ./configure: error: SSL modules require the OpenSSL library.则执行
yum -y install openssl openssl-devel
./configure
./configure --with-http_ssl_module
12345
3.执行 make(切记不能 make install 会覆盖安装目录)
4.将原来 nginx 备份
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
1
5.将新的 nginx 覆盖旧安装目录
cp objs/nginx /usr/local/nginx/sbin/nginx
1
如果报错,执行 cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -t
1
如图最后是测试成功的,之前遇到一个错误是我的SSL证书路径有错,修改后测试通过
前端页面不显示图片只显示文字内容
原因:配置文件中未配置
include /etc/nginx/mime.types;
default_type application/octet-stream;
如果不指定mime type,则默认会以text/plain的形式处理,也就是说,浏览器会以纯文本的形式来处理css和js文件,所以无法正常加载样式。
解决方法:
将该配置加入页面配置文件中重启nginx及可。