一、Linux下安装配置nginx
第一次安装nginx,中间出现的问题一步步解决。
用到的工具secureCRT,连接并登录服务器。
1.1 rz命令,会弹出会话框,选择要上传的nginx压缩包。
#rz
1.2 解压
[root@vw010001135067 ~]# cd /usr/local/
[root@vw010001135067 local]# tar -zxvf nginx-1.10.2.tar.gz
1.3 进入nginx文件夹,执行./configure命令
[root@vw010001135067 local]# cd nginx-1.10.2
[root@vw010001135067 nginx-1.10.2]# ./configure
报错如下:
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
出现这个错误。那么就是gcc 包没有安装。
1.3.1 安装gcc
查看gcc
[root@vw010001135067 nginx-1.10.2]# whereis gcc
gcc:
安装gcc
[root@vw010001135067 nginx-1.10.2]# whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz
gcc安装好了。
1.3.2 继续执行./configure
[root@vw010001135067 nginx-1.10.2]# ./configure
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... found
......
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
出现如上错误。安装pcre-devel
[root@vw010001135067 nginx-1.10.2]# yum install pcre-devel
1.3.3 再次执行./configure
error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
如果有这个错误 那么执行
yum install zlib-devel
1.3.4 执行./configure后没有报错
[root@vw010001135067 nginx-1.10.2]# ./configure
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
.......
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
1.4 如果你想使用openssl 功能,sha1 功能。 那么安装openssl ,sha1 吧
[root@vw010001135067 nginx-1.10.2]# yum install openssl openssl-devel
[root@vw010001135067 nginx-1.10.2]# install perl-Digest-SHA1.x86_64
1.4.1 开启ssl 模块 执行./configure –with-http_ssl_module
[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_ssl_module
1.4.2 启用“server+status”页,执行./configure –with-http_stub_status_module
[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module
上面两个命令同时启动可以
代码如下:
[root@vw010001135067 nginx-1.10.2]# ./configure --with-http_stub_status_module --with-http_ssl_module
1.5 上面configure就通过了
执行make 命令,执行make install 命令
[root@vw010001135067 nginx-1.10.2]# make
[root@vw010001135067 nginx-1.10.2]# make install
至此,nginx 执行成功了
1.6 配置环境变量
在/etc/profile 中加入配置
打开配置文件
[root@vw010001135067 nginx-1.10.2]# vi /etc/profile
在配置文件中加入
#nginx configure
export NGINX_HOME=/usr/local/nginx-1.10.2
export PATH=$PATH:$NGINX_HOME/sbin
我开始像上面填写,结果nginx -v的时候查找不到。注意到上面我的nginx_home配置的地址不对。先找到nginx的安装地址
[root@vw010001135067 nginx-1.10.2]# whereis nginx
nginx: /usr/local/nginx
还真是地址写错了,把上面的改成
#nginx configure
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
编译完保存退出并执行
[root@vw010001135067 nginx-1.10.2]# source /etc/profile
使配置生效。
1.7 查看nginx版本
[root@vw010001135067 nginx]# nginx -v
nginx version: nginx/1.10.2
整个过程成功了!
二、修改nginx.conf
2.1 启动nginx
我的nginx服务在http://10.1.135.67/,配置成功后,现在启动nginx
[root@vw010001135067 nginx]# cd /usr/local/nginx
[root@vw010001135067 nginx]# nginx -c conf/nginx.conf
启动成功,在浏览器打开http://10.1.135.67/,默认端口号80.
如上图,nginx已经正常工作了。
2.2 配置tomcat服务
现在我的tomcat服务在10.1.29.15,需要通过nginx转发。那么打开nginx.conf,修改配置文件。如下,添加:
#user nobody;#指定使用的用户
worker_processes 1;#开启的进程数,一般设置CUP核心数 或者核心数量*2
#定义错误日志,以及记录的日志等级
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;#单个进程最大连接数(最大连接数=连接数*进程数)
#use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
#use epoll; #使用epoll(linux2.6的高性能方式)
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
#指定日志格式
#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 on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;#该选项用于防止网络阻塞
#keepalive_timeout 0;
keepalive_timeout 65; #长连接超时时间,单位是秒
gzip on;#启用Gizp压缩
#服务器的集群
upstream log.5i5j.com { #服务器集群名字
#server 172.16.21.13:8081 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
#server 127.0.0.1:8080 weight=1;
server 10.9.2.49:28778 weight=1;
}
#当前的Nginx的配置
server {
listen 80;#监听80端口,可以改成其他端口
server_name log.5i5j.com;#当前服务的域名
#charset koi8-r;#默认字符编码
#access_log logs/host.access.log main;
#pass路径匹配 能够匹配路径中带“/”的 不过需要注意的是如果之后也有相关“/”匹配项并且使用比此处更精确匹配符,则以之后表达式优先
#location / {
# root html;
# index index.html index.htm;access_log off;
# access_log off; #关闭日志
#}
location / {
proxy_pass http://log.5i5j.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect default;
proxy_connect_timeout 9000; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 9000; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 9000; #连接成功后,后端服务器响应时间(代理接收超时)
}
location ~ ^/WEB-INF/ {
deny all;
}
#图片缓存时间设置
#location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
# expires 10d;
# root /opt/webRoot/xxx.xxx.com.static.images;
#}
#JS和CSS缓存时间设置
#location ~ .*.(js|css)?$ {
# expires 1h;
# root /opt/webRoot/xxx.xxx.com.static.js;
#}
#对 "/" 启用反向代理
#location / {
# proxy_pass http://127.0.0.1:88;
# proxy_redirect off;
# proxy_set_header X-Real-IP $remote_addr;
# #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# #以下是一些反向代理的配置,可选。
# proxy_set_header Host $host;
# client_max_body_size 10m; #允许客户端请求的最大单文件字节数
# client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
# proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
# proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
# proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
# proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
# proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
# proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
# proxy_temp_file_write_size 64k;
# #设定缓存文件夹大小,大于这个值,将从upstream服务器传
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html { # "/"精确的匹配,并且不再向下匹配
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { # "~ $"正则表达式匹配 一旦匹配则不再向下匹配
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
#location ~* \.(txt|doc)$ { # "~*" 代表不区分大小写方式匹配
# deny all;#不希望访问txt|doc文件
#}
}
}
配置好后,保存配置文件,并且重启nginx
[root@vw010001135067 nginx]# nginx -s reload
在浏览器调用upload项目是否成功
如图能正确访问项目,配置成功!
原文链接:http://blog.csdn.net/u010416588/article/details/54379282