nginx的反向代理,域名转发

一、环境安装

  系统:centos7

  nginx下载地址:https://nginx.org/en/download.html

  下载 PCRE 安装包,下载地址:https://sourceforge.net/projects/pcre/files/pcre/

[root@zrl tool]# yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel #安装编译工具及库文件
[root@zrl tool]# wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz #下载PCRE包(PCRE 作用是让 Nginx 支持 Rewrite 功能)
[root@zrl tool]# wget https://nginx.org/download/nginx-1.19.5.tar.gz  #nginx下载

二、编译配置

pcre的编译安装

[root@zrl pcre-8.35]# ./configure #检测平台特征,生成Makefile,为下面make准备
[root@zrl pcre-8.35]# make && make install #编译 且 安装
[root@zrl pcre-8.35]# pcre-config --version #检测版本

nginx的编译安装

[root@zrl nginx-1.19.5]# ./configure --prefix=/tool/nginx-1.19.5/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/tool/pcre-8.44
[root@zrl nginx-1.19.5]# make
[root@zrl nginx-1.19.5]# make install 
[root@zrl nginx-1.19.5]# cd /tool/nginx-1.19.5/nginx/sbin
[root@zrl sbin]# ./nginx -v #查看版本(linux系统 nginx -v)

创建 Nginx 运行使用的用户 www (也可以直接使用root)

[root@zrl conf]# /usr/sbin/groupadd www 
[root@zrl conf]# /usr/sbin/useradd -g www www

 

三、配置nginx的config文件

user  www; #使用www用户
worker_processes  1;  #设置值和CPU核心数一致

error_log /tool/nginx-1.19.5/nginx/logs/nginx_error.log crit; #日志位置和日志级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid /tool/nginx-1.19.5/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535; #指定此进程可以打开的最大文件描述符的值

events {
  use epoll;
  worker_connections 65535; #连接数
}


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;

    # 下面是server虚拟主机的配置
    # 下面为转发到8080端口
    server {
        listen       80;
        server_name  xxx.xin; #配置域名跟ip亦可,多个域名用空格隔开 e.g:www.xxx.com wx.xxx.com

        #charset koi8-r;

        location / { #请求根目录
            proxy_pass   http://localhost:8080; #转发的地址
            proxy_redirect off; #重定向
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {#错误页面
            root   html;
        }

    }
    # 下面为转发到8081端口
    server {
        listen       80;
        server_name  xxx.net;

        location / {
            proxy_pass   http://localhost:8081; #转发的地址
            proxy_redirect off; #重定向
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {#错误页面
            root   html;
        }
    }

}

 

按照上面的方法,若有很N个服务器,管理起来有点吃力。(服务器记得把80、433端口开放,否则也访问不了!)

nginx提供了拆分的正则,使用 include vhost/*.conf

nginx.conf的配置

user  www; #使用www用户
worker_processes  1;  #设置值和CPU核心数一致

error_log /tool/nginx-1.19.5/nginx/logs/nginx_error.log crit; #日志位置和日志级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid /tool/nginx-1.19.5/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535; #指定此进程可以打开的最大文件描述符的值

events {
  use epoll;
  worker_connections 65535; #连接数
}


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;
    
    include vhost/*.conf # 将vhost文件夹下所有配置文件合并到这里

}


在config目录下,创建vhost文件夹,再创建文件xxx.xin.8080.conf(最好使用域名+端口,否则容易记混)

server {
    listen       80;
    server_name  xxx.xin; #配置域名跟ip亦可,多个域名用空格隔开 e.g:www.xxx.com wx.xxx.com

    #charset koi8-r;
    location = /50x.html {#错误页面
        root   html;
    }

    location / { #请求根目录
        proxy_pass   http://127.0.0.1:8080; #转发的地址
        proxy_redirect off; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
    }
}

再创建xxx.net.8081.conf文件

server {
    listen       80;
    server_name  xxx.net; 

    #charset koi8-r;

    location = /50x.html {#错误页面
        root   html;
    }

    location / { #请求根目录
        proxy_pass   http://127.0.0.1:8081; #转发的地址
        proxy_redirect off; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
    }
}

这样完全跟前面全部写到nginx.conf文件中的效果一样。

四、HTTPS请求

 将xxx.net.8081.conf改成https请求

server {
  listen   80; # 可以http+https listen
443 ssl; server_name xxx.net; ssl_certificate xxx.net.pem; ssl_certificate_key xxx.net.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
location
/ { #请求根目录 proxy_pass http://localhost:8081; #转发的地址 proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; } }

 

五、nginx命令

[root@zrl /]# ./tool/webserver/nginx/sbin/nginx -t #检查配置文件
[root@zrl /]# ./tool/webserver/nginx/sbin/nginx -v #查看版本 [root@zrl /]# .
/tool/webserver/nginx/sbin/nginx -c /tool/webserver/nginx/config/nginx.conf #启动并使用nginx.conf的配置文件(nginx.conf文件一定要使用绝对路径,否则会出错!!!) [root@zrl /]# ./tool/webserver/nginx/sbin/nginx -s reload #重新载入配置文件(若报错,可以尝试重新 -c nginx.conf 再 -s reload) [root@zrl /]# ./tool/webserver/nginx/sbin/nginx -s stop #停止服务 [root@zrl /]# ./tool/webserver/nginx/sbin/nginx -s reopen #重启服务

 

六、开机启动配置

  在/usr/lib/systemd/system目录下新建nginx.service文件,内容如下

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
 
[Install]
WantedBy=multi-user.target

  开启nginx开机命令

刷新配置system目录
sudo systemctl daemon-reload

设置开机自启动: systemctl enable nginx.service
查看是否正确启动: systemctl list
-unit-files |grep nginx

 

  关于systemctl命令


  开启开机自启动:systemctl enable nginx.service
  停止开机自启动 : systemctl disable nginx.service
  启动 nginx 服务 : systemctl start nginx.service
  停止 nginx 服务 : systemctl stop nginx.service
  重启 nginx 服务 : systemctl restart nginx.service
  查看服务当前状态 : systemctl status nginx.service
  查看所有已启动的服务 : systemctl list-units --type=service

 

 

七、总结

  之前多个项目多个域名配置很麻烦,但用了nginx后,域名+项目平时也更好的运维管理。nginx在占资源方面也非常的低,配置生效也非常快。在HTTP跟HTTPS都有很好的支持,还有在负载均衡上也可以进行权重配置,还有静态文件缓存等等。总之Nginx很强大。

posted @ 2020-12-11 08:23  Auler  阅读(661)  评论(0编辑  收藏  举报