系统平台:CentOS release 6.5 (Final) 64位。
安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
首先要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
路由重写是Web服务器中的一个很重要的基本功能。通过路由重写,可以结构化URL,更具语义化(对SEO有益)。另外,分享出去的URL可能会因程序路由变动而导致URL失效,而路由的重写可以很好的解决这类问题。
适当的使用Rewrite功能,可以更我们带来很多的好处。Nginx中Rewrite的功能是基于perl语言兼容的正则表达式,所以在编译安装nginx之前,需要安装PREC库。Nginx中Rewrite功能实现是基于ngx_http_rewrite_module,所以确保安装了此模块。
1.下载 PCRE 安装包,下载地址: Download PCRE from SourceForge.net
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2.解压安装包:
[root@bogon src]# tar zxvf pcre-8.35.tar.gz
3.进入安装包目录
[root@bogon src]# cd pcre-8.35
4.编译安装
[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install
5.查看pcre版本
[root@bogon pcre-8.35]# pcre-config --version
安装 Nginx
1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.17.5.tar.gz
[root@bogon src]# wget http://nginx.org/download/nginx-1.17.5.tar.gz
2、解压安装包
[root@bogon src]# tar zxvf nginx-1.17.5.tar.gz
3、进入安装包目录
[root@bogon src]# cd nginx-1.17.5
4、编译安装
x需要提前安装好pcre-devel和openssl-devel
yum -y install pcre-devel openssl openssl-devel
否则会报错误提示:
./configure: error: the HTTP rewrite module requires the PCRE library.
./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=<path> options.
然后再编译
注意这一步是在解压后的文件夹内编译, --prefix后面的目录是空的安装目录,不能是同一个目录,例如解压且要编译的目录是/opt/soft/nginx,安装目录在/opt/module/nginx-1.17.5
否则会报错cp: ‘conf/koi-win‘ and ‘xxx/conf/koi-win‘ are the same file
[root@bogon nginx-1.17.5]# ./configure --prefix=/opt/module/nginx-1.17.5 --conf-path=/opt/module/nginx-1.17.5/conf/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/module/pcre-8.35
[root@bogon nginx-1.17.5]# make && make install
5、查看nginx版本
[root@bogon nginx1.17.5]# ./sbin/nginx -v
到此,nginx安装完成。
Nginx 配置
创建 Nginx 运行使用的用户 www:
[root@bogon conf]# /usr/sbin/groupadd www
[root@bogon conf]# /usr/sbin/useradd -g www www
配置nginx.conf ,将/opt/module/nginx-1.17.5/conf/nginx.conf替换为以下内容
[root@bogon conf]# cat /opt/module/nginx-1.17.5/conf/nginx.conf
#user nobody;
worker_processes 1;
#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;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
}
}
检查配置文件ngnix.conf的正确性命令:
[root@bogon conf]# /opt/module/nginx-1.17.5/sbin/nginx -t
启动 Nginx
Nginx 启动命令如下:
[root@bogon conf]# /opt/module/nginx-1.17.5/sbin/nginx
如果报错找不到error和access错误日志的路径,则在nginx安装目录下新建一个logs文件夹就可以搞定
访问站点
从浏览器访问我们配置的站点ip:
192.168.0.XXX
Nginx 其他命令
以下包含了 Nginx 常用的几个命令:
/opt/module/nginx-1.17.5/sbin/nginx -s reload # 重新载入配置文件
/opt/module/nginx-1.17.5/sbin/nginx -s reopen # 重启 Nginx
/opt/module/nginx-1.17.5/sbin/nginx -s stop # 停止 Nginx
修改nginx.conf 并重启
1.把nginx.conf修改为:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
pid /usr/local/webserver/nginx/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 配置web集群
upstream bakend {
#ip_hash;
server 192.168.0.204:8080 weight=1;
server 192.168.0.170:8080 weight=1;
}
server {
listen 80;
server_name localhost;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
#proxy_pass http://192.168.0.170:8080;
proxy_pass http://bakend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location / {
#proxy_pass http://192.168.0.170:8080/;
proxy_pass http://bakend/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
}
}
2.重启Nginx
/usr/local/webserver/nginx/sbin/nginx -s reopen
3. 从浏览器访问我们配置的站点:192.168.0.XXX如下图 部署成功