一、 环境
操作系统版本:CentOS Linux release 7.4.1708 (Core) 最小化安装版
cat /etc/redhat-release
二、 安装过程
1. 安装编译工具及相关库。
[root@Geeklp-Nginx ~]# yum -y installgcc-c++ zlib-devel openssl-devel libtool
2. 下载Nginx及PCRE。
[root@Geeklp-Nginx ~]# curl -C - -O http://mirrors.sohu.com/nginx/nginx-1.13.7.tar.gz
[root@Geeklp-Nginx ~]# curl -C - -O https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
3. 编译安装PCRE。
[root@Geeklp-Nginx ~]# tar -xvzfpcre-8.41.tar.gz
[root@Geeklp-Nginx pcre-8.41]# cd pcre-8.41
[root@Geeklp-Nginx pcre-8.41]# mkdir/usr/local/share/applications/pcre-8.41
[root@Geeklp-Nginx pcre-8.41]# ./configure--prefix=/usr/local/share/applications/pcre-8.41/
[root@Geeklp-Nginx pcre-8.41]# make&& make install
4. 编译安装Nginx。
[root@Geeklp-Nginx ~]# tar -xvzfnginx-1.13.7.tar.gz
[root@Geeklp-Nginx nginx-1.13.7]# cdnginx-1.13.7
[root@Geeklp-Nginx nginx-1.13.7]#./configure --prefix=/usr/local/share/applications/nginx-1.13.7/--with-http_ssl_module
[root@Geeklp-Nginx nginx-1.13.7]# make&& make install
5. 启动Nginx。
[root@Geeklp-Nginx nginx-1.13.7]#/usr/local/share/applications/nginx-1.13.7/sbin/nginx
6. 配置防火墙。
[root@Geeklp-Nginx nginx-1.13.7]#firewall-cmd --permanent --add-port=80/tcp
[root@Geeklp-Nginx nginx-1.13.7]#firewall-cmd --reload
7. 验证。
在浏览器中输入服务器地址:10.250.151.234。出现如下图所示则说明Nginx安装成功!
这里写图片描述
三、 配置Nginx
1. 查看Nginx版本:
[root@Geeklp-Nginx nginx-1.13.7]#/usr/local/share/applications/nginx-1.13.7/sbin/nginx -v
nginx version: nginx/1.13.7
2. 修改配置文件nginx.conf。
黄色部分是配置关键
[root@localhostconf]# more nginx.conf
#user nobody;
user root;
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 8888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /admin/ {
proxy_pass http://127.0.0.1:9080/admin/;
proxy_redirect default;
}
location/gy-mobile/ {
client_max_body_size 200m;
proxy_pass http://127.0.0.1:9180/gy-mobile/;
proxy_redirect default;
}
location/images/ {
root /;
rewrite^/images/(.*)$ /home/dangjian/upload/images/$1 break;
}
location/upload/ {
root /;
rewrite^/upload/(.*)$ /home/dangjian/upload/$1 break;
}
location /dpp-admin/ {
proxy_pass http://127.0.0.1:8081/dpp-admin/;
client_max_body_size 200m;
proxy_redirect default;
}
location /dpp-mobile/{
proxy_pass http://127.0.0.1:8083/dpp-mobile/;
client_max_body_size 200m;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to thestatic page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@localhostconf]#
3. 重载配置文件,并配置防火墙,主要解决新增端口问题。
[root@Geeklp-Nginx conf]# ../sbin/nginx -sreload
[root@Geeklp-Nginx conf]# firewall-cmd--permanent --add-port=8888/tcp
[root@Geeklp-Nginx conf]# firewall-cmd--reload
4. 停止Nginx。
[root@Geeklp-Nginx conf]# ../sbin/nginx -sstop
5. 启动Nginx并再次重启Nginx。
[root@Geeklp-Nginx conf]# ../sbin/nginx
[root@Geeklp-Nginx conf]# ../sbin/nginx -sreopen
6. 查看帮助。
[root@Geeklp-Nginx conf]# ../sbin/nginx -h
nginx version: nginx/1.13.7
Usage: nginx [-?hvVtTq] [-s signal] [-cfilename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version andexit
-V : show version andconfigure options then exit
-t : test configurationand exit
-T : testconfiguration, dump it and exit
-q : suppress non-errormessages during configuration testing
-ssignal : send signal to a masterprocess: stop, quit, reopen, reload
-pprefix : set prefix path (default:/usr/local/share/applications/nginx-1.13.7//)
-cfilename : set configuration file(default: conf/nginx.conf)
-gdirectives : set global directives out of configuration file
7. 配置开机启动。
[root@Geeklp-Nginx sbin] echo"/usr/local/share/applications/nginx-1.13.7/sbin/nginx">>/etc/rc.local
[root@Geeklp-Nginx sbin]# chmod u+x/etc/rc.d/rc.local
四、 配置多个service
其中主要的是有两个server,每个server对应的被代理的服务器的不同。从而实现了nginx代理多个服务器的目的。
修改$nginx_home/conf下面的nginx.conf文件
下面是两个服务server的配置(查看黄色部分):
[root@localhostconf]# more nginx.conf
#user nobody;
user root;
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;
#映射项目1外网进来的端口8888指定到9080、9180、8081、8083
server {
listen 8888;
server_name localhost;
#charsetkoi8-r;
#access_log logs/host.access.log main;
location /{
root html;
index index.html index.htm;
}
location /admin/ {
proxy_pass http://127.0.0.1:9080/admin/;
proxy_redirect default;
}
location/gy-mobile/ {
client_max_body_size 200m;
proxy_pass http://127.0.0.1:9180/gy-mobile/;
proxy_redirect default;
}
location/images/ {
root /;
rewrite^/images/(.*)$ /home/dangjian/upload/images/$1 break;
}
location/upload/ {
root /;
rewrite^/upload/(.*)$ /home/dangjian/upload/$1 break;
}
location /dpp-admin/ {
proxy_pass http://127.0.0.1:8081/dpp-admin/;
client_max_body_size 200m;
proxy_redirect default;
}
location/dpp-mobile/ {
proxy_pass http://127.0.0.1:8083/dpp-mobile/;
client_max_body_size 200m;
proxy_redirect default;
}
#error_page 404 /404.html;
# redirectserver error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
}
#映射项目2外网进来的后端端口8378指定到8379
server {
listen 8378;
server_name BusinesServer;
location /{
root html;
index index.html index.htm;
proxy_passhttp://localhost:8379;
proxy_redirect default;
}
error_page 500 502 503 504 /50x.html;
location =/50x.html {
root html;
}
}
}
[root@localhostconf]#