«

Centos7安装部署NGINX,做反向代理

时间:2022-10-14 00:04     作者:Anglei     分类: Linux


gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装。

yum install gcc-c++ 

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

yum install -y pcre pcre-devel 

zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel 

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel 

二、安装Nginx

# 下载源码包 
cd /opt 
wget http://nginx.org/download/nginx-1.15.9.tar.gz 
# 解压缩源码 
tar -xzvf nginx-1.15.9.tar.gz 
# 配置,编译安装(开启nginx状态监测功能) 
cd nginx-1.15.9/ 
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-stream
make && make install 
# 启动nginx,进入sbin目录,找到nginx启动命令 
./nginx # 开启niginx 
./nginx -s stop # 相当于先查出nginx进程id再使用kill命令强制杀掉进程 
./nginx -s quit # 待nginx进程处理任务完毕进行停止 
./nginx -s reload # 重新加载配置 

开启防火墙80端口的外部访问:

firewall-cmd --zone=public --add-port=80/tcp --permanent 
firewall-cmd --reload 

启动成功后,在浏览器输入服务器IP,就可以看到这样的页面了:

设置开机自启

在 rc.local 添加启动代码即可:

vim /etc/rc.local

在最后一行添加:

/opt/nginx/sbin/nginx

设置文件的执行权限:

chmod 755 /etc/rc.local

至此,nginx就安装完毕了。除此之外,也可以设置nginx为服务并开机自启,具体请自行解决。

对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  shineflyimages.shenfeiyun.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
 #    proxy_pass https://152.136.1.1;
        #    root   html;
        #    index  index.html index.htm index.jsp;
        #}

        #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;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  XXX.XXX.COM;

        ssl_certificate server.crt; 
        ssl_certificate_key server.key; 

        ssl_session_timeout 5m;
        ssl_protocols TLSv1.2 TLSv1.3; 

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass https://152.136.1.1;
            root   html;
            index  index.html index.htm index.jsp;
        }
    }

   #server {
   #     listen       80;
   #     server_name  csvmotion.shenfeiyun.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

   #     location / {
   #         proxy_pass https://49.232.88.145;
   #         root   html;
   #         index  index.html index.htm index.jsp;
   #     }

#}

    server {
        listen       443 ssl;
        server_name  xxx.xxx.com;

        ssl_certificate server.crt;
        ssl_certificate_key server.key;

        ssl_session_timeout 5m;
        ssl_protocols TLSv1.2 TLSv1.3;

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass https://49.232.8.8;
            root   html;
            index  index.html index.htm index.jsp;
        }
    }
}

stream {
       upstream gateway1 {
               server 152.136.2.2:8088;
 server 152.136.2.3:8088;
       }
       server {
               listen 8088;
               proxy_pass      gateway1;
       }
       upstream gateway2 {
               server 152.136.2.2:8090;
 server 152.136.2.3:8090;
       }
       server {
               listen 8090;
               proxy_pass      gateway2;
       }
       upstream gateway3 {
               server 152.136.2.2:2888;
 server 152.136.2.3:2888;
       }
       server {
               listen 2888;
               proxy_pass      gateway3;
       }
       upstream gateway4 {
               server 152.136.2.2:3888;
 server 152.136.2.3:3888;
       }
       server {
               listen 3888;
               proxy_pass      gateway4;
       }
}

本文完结,相关标签: nginx

 版权所有:Anglei
 文章标题:Centos7安装部署NGINX,做反向代理
 除非注明,本站文章如未特殊说明均为 MAXADA社区知识库 原创,且版权所有,请勿用于任何商业用途。

推荐阅读:

看完后感想如何?

路过(0)

雷人(0)

握手(0)

鲜花(0)

鸡蛋(0)
分享到: