二肥 發表於 2020-12-11 08:23:00

nginx的反向代理,域名转发

<h2>一、环境安装</h2>
<p>  系统:centos7</p>
<p>  nginx下载地址:https://nginx.org/en/download.html</p>
<p>  下载 PCRE 安装包,下载地址:https://sourceforge.net/projects/pcre/files/pcre/</p>
<div class="cnblogs_code">
<pre># <span style="color: rgba(0, 0, 255, 1)">yum</span> -y <span style="color: rgba(0, 0, 255, 1)">install</span> <span style="color: rgba(0, 0, 255, 1)">make</span> zlib zlib-devel <span style="color: rgba(0, 0, 255, 1)">gcc</span>-c++ libtoolopenssl openssl-<span style="color: rgba(0, 0, 0, 1)">devel <span style="color: rgba(255, 0, 0, 1)">#安装编译工具及库文件</span>
# wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz</span><span style="color: rgba(0, 128, 0, 1)"> <span style="color: rgba(255, 0, 0, 1)">#下载PCRE包(PCRE 作用是让 Nginx 支持 Rewrite 功能)</span></span>
# <span style="color: rgba(0, 0, 255, 1)">wget</span> https://nginx.org/download/nginx-1.19.5.tar.gz<span style="color: rgba(0, 128, 0, 1)"><span style="color: rgba(255, 0, 0, 1)">#nginx下载</span></span></pre>
</div>
<h2>二、编译配置</h2>
<p>pcre的编译安装</p>
<div class="cnblogs_code">
<pre># ./<span style="color: rgba(0, 0, 0, 1)">configure <span style="color: rgba(255, 0, 0, 1)">#检测平台特征,生成Makefile,为下面make准备</span>
# <span style="color: rgba(0, 0, 255, 1)">make</span> &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install <span style="color: rgba(255, 0, 0, 1)">#编译 且 安装<br></span></span><span class="pun">[<span class="pln">root@zrl pcre<span class="pun">-<span class="lit">8.35<span class="pun">]#<span class="pln"> pcre<span class="pun">-<span class="pln">config <span class="pun">--<span class="pln">version<span style="color: rgba(255, 0, 0, 1)"> #检测版本</span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p>nginx的编译安装</p>
<div class="cnblogs_code">
<pre># ./configure --prefix=/tool/nginx-1.19.5/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/tool/pcre-<span style="color: rgba(128, 0, 128, 1)">8.44</span><span style="color: rgba(0, 0, 0, 1)">
# <span style="color: rgba(0, 0, 255, 1)">make</span><span style="color: rgba(0, 0, 0, 1)">
# <span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span> <br># cd /tool/nginx-1.19.5/nginx/sbin<br># ./nginx -v #查看版本(linux系统 nginx -v)</pre>
</div>
<p>创建 Nginx 运行使用的用户 www (也可以直接使用root)</p>
<div class="cnblogs_code">
<pre># /usr/sbin/<span style="color: rgba(0, 0, 0, 1)">groupadd www
# </span>/usr/sbin/useradd -g www www</pre>
</div>
<p>&nbsp;</p>
<h2>三、配置nginx的config文件</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">userwww; #使用www用户
worker_processes</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;#设置值和CPU核心数一致

error_log </span>/tool/nginx-<span style="color: rgba(128, 0, 128, 1)">1.19</span>.<span style="color: rgba(128, 0, 128, 1)">5</span>/nginx/logs/<span style="color: rgba(0, 0, 0, 1)">nginx_error.log crit; #日志位置和日志级别
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.lognotice;
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.loginfo;

pid </span>/tool/nginx-<span style="color: rgba(128, 0, 128, 1)">1.19</span>.<span style="color: rgba(128, 0, 128, 1)">5</span>/nginx/<span style="color: rgba(0, 0, 0, 1)">nginx.pid;

#Specifies the value </span><span style="color: rgba(0, 0, 255, 1)">for</span> maximum file descriptors that can be opened by <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"> process.
worker_rlimit_nofile </span><span style="color: rgba(128, 0, 128, 1)">65535</span><span style="color: rgba(0, 0, 0, 1)">; #指定此进程可以打开的最大文件描述符的值

events {
use epoll;
worker_connections </span><span style="color: rgba(128, 0, 128, 1)">65535</span><span style="color: rgba(0, 0, 0, 1)">; #连接数
}


http {
    include       mime.types;
    default_typeapplication</span>/octet-<span style="color: rgba(0, 0, 0, 1)">stream;

    log_formatmain</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: rgba(128, 0, 0, 1)">'</span>
                     <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$status $body_bytes_sent "$http_referer" </span><span style="color: rgba(128, 0, 0, 1)">'</span>
                      <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">"$http_user_agent" "$http_x_forwarded_for"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

    #access_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">access.logmain;

    # 下面是server虚拟主机的配置
    # 下面为转发到8080端口
    server {
      listen       </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
      server_namexxx.xin; #配置域名跟ip亦可,多个域名用空格隔开 e.g:www.xxx.com wx.xxx.com

      #charset koi8</span>-<span style="color: rgba(0, 0, 0, 1)">r;

      location </span>/<span style="color: rgba(0, 0, 0, 1)"> { #请求根目录
            proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:8080; #转发的地址</span>
<span style="color: rgba(0, 0, 0, 1)">            proxy_redirect off; #重定向
            proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for;
            proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr;
            proxy_set_header Host $http_host;
      }

      error_page   </span><span style="color: rgba(128, 0, 128, 1)">500</span> <span style="color: rgba(128, 0, 128, 1)">502</span> <span style="color: rgba(128, 0, 128, 1)">503</span> <span style="color: rgba(128, 0, 128, 1)">504</span>/<span style="color: rgba(0, 0, 0, 1)">50x.html;

      location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {#错误页面
            root   html;
      }

    }
    # 下面为转发到8081端口
    server {
      listen       </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
      server_namexxx.net;

      location </span>/<span style="color: rgba(0, 0, 0, 1)"> {
            proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:8081; #转发的地址</span>
<span style="color: rgba(0, 0, 0, 1)">            proxy_redirect off; #重定向
            proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for;
            proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr;
            proxy_set_header Host $http_host;
      }

      error_page   </span><span style="color: rgba(128, 0, 128, 1)">500</span> <span style="color: rgba(128, 0, 128, 1)">502</span> <span style="color: rgba(128, 0, 128, 1)">503</span> <span style="color: rgba(128, 0, 128, 1)">504</span>/<span style="color: rgba(0, 0, 0, 1)">50x.html;

      location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {#错误页面
            root   html;
      }
    }

}</span></pre>
</div>
<p>&nbsp;</p>
<p>按照上面的方法,若有很N个服务器,管理起来有点吃力。<span style="color: rgba(255, 0, 0, 1)">(服务器记得把80、433端口开放,否则也访问不了!)</span></p>
<p>nginx提供了拆分的正则,使用&nbsp;include vhost/*.conf</p>
<p>nginx.conf的配置</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">userwww; #使用www用户
worker_processes</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;#设置值和CPU核心数一致

error_log </span>/tool/nginx-<span style="color: rgba(128, 0, 128, 1)">1.19</span>.<span style="color: rgba(128, 0, 128, 1)">5</span>/nginx/logs/<span style="color: rgba(0, 0, 0, 1)">nginx_error.log crit; #日志位置和日志级别
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.lognotice;
#error_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">error.loginfo;

pid </span>/tool/nginx-<span style="color: rgba(128, 0, 128, 1)">1.19</span>.<span style="color: rgba(128, 0, 128, 1)">5</span>/nginx/<span style="color: rgba(0, 0, 0, 1)">nginx.pid;

#Specifies the value </span><span style="color: rgba(0, 0, 255, 1)">for</span> maximum file descriptors that can be opened by <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"> process.
worker_rlimit_nofile </span><span style="color: rgba(128, 0, 128, 1)">65535</span><span style="color: rgba(0, 0, 0, 1)">; #指定此进程可以打开的最大文件描述符的值

events {
use epoll;
worker_connections </span><span style="color: rgba(128, 0, 128, 1)">65535</span><span style="color: rgba(0, 0, 0, 1)">; #连接数
}


http {
    include       mime.types;
    default_typeapplication</span>/octet-<span style="color: rgba(0, 0, 0, 1)">stream;

    log_formatmain</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: rgba(128, 0, 0, 1)">'</span>
                     <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$status $body_bytes_sent "$http_referer" </span><span style="color: rgba(128, 0, 0, 1)">'</span>
                      <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">"$http_user_agent" "$http_x_forwarded_for"</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">;

    #access_loglogs</span>/<span style="color: rgba(0, 0, 0, 1)">access.logmain;
   
    include vhost</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">.conf; # 将vhost文件夹下所有配置文件合并到这里,<span style="color: rgba(255, 0, 0, 1)">conf后面要加 ; 号</span>

}</span></pre>
</div>
<p><br>在config目录下,创建vhost文件夹,再创建文件xxx.xin.8080.conf(最好使用域名+端口,否则容易记混)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">server {
    listen       </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
    server_namexxx.xin; #配置域名跟ip亦可,多个域名用空格隔开 e.g:www.xxx.com wx.xxx.com

    #charset koi8</span>-<span style="color: rgba(0, 0, 0, 1)">r;</span><span style="color: rgba(0, 0, 0, 1)">
    location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {#错误页面
      root   html;
    }
</span><span style="color: rgba(0, 0, 0, 1)">
    location </span>/<span style="color: rgba(0, 0, 0, 1)"> { #请求根目录
      proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">127.0.0.1:8080; #转发的地址</span>
<span style="color: rgba(0, 0, 0, 1)">      proxy_redirect off;
      proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for;
      proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr;
      proxy_set_header Host $http_host;
    }
}</span></pre>
</div>
<p>再创建xxx.net.8081.conf文件</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">server {
    listen       </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
    server_namexxx.net;

    #charset koi8</span>-<span style="color: rgba(0, 0, 0, 1)">r;

    location </span>= /<span style="color: rgba(0, 0, 0, 1)">50x.html {#错误页面
      root   html;
    }

    location </span>/<span style="color: rgba(0, 0, 0, 1)"> { #请求根目录
      proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">127.0.0.1:8081; #转发的地址</span>
<span style="color: rgba(0, 0, 0, 1)">      proxy_redirect off;
      proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for;
      proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr;
      proxy_set_header Host $http_host;
    }
}</span></pre>
</div>
<p>这样完全跟前面全部写到nginx.conf文件中的效果一样。</p>
<h2>四、HTTPS请求</h2>
<p>&nbsp;将xxx.net.8081.conf改成https请求</p>
<div class="cnblogs_code">
<pre># HTTP server block -<span style="color: rgba(0, 0, 0, 1)"> 用于处理所有HTTP请求并将其重定向到HTTPS
server {
    listen </span><span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
    server_name xxx.com;

    # 将所有HTTP请求重定向到HTTPS
    rewrite </span>^(.*)$ https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$host$1 permanent; # 或者使用 return 301 https:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$host$request_uri;</span>
<span style="color: rgba(0, 0, 0, 1)">}

# HTTPS server block </span>-<span style="color: rgba(0, 0, 0, 1)"> 处理所有HTTPS请求
server {
    listen </span><span style="color: rgba(128, 0, 128, 1)">443</span><span style="color: rgba(0, 0, 0, 1)"> ssl;
    server_name xxx.com;

    ssl_certificate      xxx</span><span style="color: rgba(0, 0, 0, 1)">.pem;
    ssl_certificate_keyxxx</span><span style="color: rgba(0, 0, 0, 1)">.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout5m;

    ssl_ciphersHIGH:</span>!aNULL:!<span style="color: rgba(0, 0, 0, 1)">MD5;
    ssl_prefer_server_cipherson;

    location </span>/<span style="color: rgba(0, 0, 0, 1)"> { # 请求根目录
      proxy_pass   http:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:8080; # 转发的地址</span>
<span style="color: rgba(0, 0, 0, 1)">      proxy_redirect off;
      proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1)">For $proxy_add_x_forwarded_for;
      proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1)">IP $remote_addr;
      proxy_set_header Host $http_host;
    }
}</span></pre>
</div>
<p>以下是错误的配置,会导致配置无法生效。</p>
<div class="cnblogs_code">
<pre><span style="text-decoration: line-through"><span style="color: rgba(0, 0, 0, 1); text-decoration: line-through">server {<br> listen   80; # 可以http+https
    listen       </span><span style="color: rgba(128, 0, 128, 1); text-decoration: line-through">443</span><span style="color: rgba(0, 0, 0, 1); text-decoration: line-through"> ssl;
    server_namexxx.net;
   
    ssl_certificate      xxx.net.pem;
    ssl_certificate_keyxxx.net.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout5m;

    ssl_ciphersHIGH:</span>!aNULL:!<span style="color: rgba(0, 0, 0, 1); text-decoration: line-through">MD5;
    ssl_prefer_server_cipherson;<br>
    rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。<br>
    location </span>/<span style="color: rgba(0, 0, 0, 1); text-decoration: line-through"> { #请求根目录
      proxy_pass   http://localhost</span><span style="color: rgba(0, 128, 0, 1); text-decoration: line-through">:8081; #转发的地址</span>
<span style="color: rgba(0, 0, 0, 1); text-decoration: line-through">      proxy_redirect off;
      proxy_set_header X</span>-Forwarded-<span style="color: rgba(0, 0, 0, 1); text-decoration: line-through">For $proxy_add_x_forwarded_for;
      proxy_set_header X</span>-Real-<span style="color: rgba(0, 0, 0, 1); text-decoration: line-through">IP $remote_addr;
      proxy_set_header Host $http_host;
    }
}</span></span></pre>
</div>
<p>&nbsp;</p>
<h2>五、nginx命令</h2>
<div class="cnblogs_code">
<pre># ./tool/webserver/nginx/sbin/nginx -<span style="color: rgba(0, 0, 0, 1)">t #检查配置文件<br># ./tool/webserver/nginx/sbin/nginx -v #查看版本
# .</span>/tool/webserver/nginx/sbin/nginx -c/tool/webserver/nginx/config/<span style="color: rgba(0, 0, 0, 1)">nginx.conf #启动并使用nginx.conf的配置文件<span style="color: rgba(255, 0, 0, 1)">(nginx.conf文件一定要使用绝对路径,否则会出错!!!)</span>
# .</span>/tool/webserver/nginx/sbin/nginx -<span style="color: rgba(0, 0, 0, 1)">s reload #重新载入配置文件(若报错,可以尝试重新 -c nginx.conf 再 -s reload)
# .</span>/tool/webserver/nginx/sbin/nginx -<span style="color: rgba(0, 0, 0, 1)">s stop #停止服务
# .</span>/tool/webserver/nginx/sbin/nginx -s reopen #重启服务</pre>
</div>
<p>&nbsp;</p>
<h2>六、开机启动配置</h2>
<p>  在/usr/lib/systemd/system目录下新建nginx.service文件,内容如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">
Description</span>=nginx -<span style="color: rgba(0, 0, 0, 1)"> high performance web server
After</span>=network.target remote-fs.target nss-<span style="color: rgba(0, 0, 0, 1)">lookup.target


Type</span>=<span style="color: rgba(0, 0, 0, 1)">forking
ExecStart</span>=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/<span style="color: rgba(0, 0, 0, 1)">nginx.conf
ExecReload</span>=/usr/local/nginx/sbin/nginx -<span style="color: rgba(0, 0, 0, 1)">s reload
ExecStop</span>=/usr/local/nginx/sbin/nginx -<span style="color: rgba(0, 0, 0, 1)">s stop


WantedBy</span>=multi-user.target</pre>
</div>
<p>  开启nginx开机命令</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">刷新配置system目录<br>sudo systemctl daemon-reload<br><br>设置开机自启动:
systemctl enable nginx.service<br>
查看是否正确启动:
systemctl list</span>-unit-files |<span style="color: rgba(0, 0, 255, 1)">grep</span> nginx</pre>
</div>
<p>&nbsp;</p>
<p>  关于systemctl命令</p>
<div class="cnblogs_code">
<p><br>  开启开机自启动:systemctl enable nginx.service<br>  停止开机自启动 : systemctl disable nginx.service<br>  启动 nginx 服务 : systemctl start nginx.service<br>  停止 nginx 服务 : systemctl stop nginx.service<br>  重启 nginx 服务 : systemctl restart nginx.service<br>  查看服务当前状态 : systemctl status nginx.service<br>  查看所有已启动的服务 : systemctl list-units --type=service</p>
<p>&nbsp;</p>




</div>
<p>&nbsp;</p>
<h2>七、总结</h2>
<p>  之前多个项目多个域名配置很麻烦,但用了nginx后,域名+项目平时也更好的运维管理。nginx在占资源方面也非常的低,配置生效也非常快。在HTTP跟HTTPS都有很好的支持,还有在负载均衡上也可以进行权重配置,还有静态文件缓存等等。总之Nginx很强大。</p><br><br>
来源:https://www.cnblogs.com/zrl66/p/14118063.html
頁: [1]
查看完整版本: nginx的反向代理,域名转发