Nginx:通過DNS與Nginx代理,實(shí)現(xiàn)本地的生產(chǎn)與開發(fā)接口切換

一、安裝

1)添加源

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2)安裝Nginx

sudo yum install -y nginx

3)啟動(dòng)Nginx并設(shè)置開機(jī)自動(dòng)運(yùn)行

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

二、配置及使用

1.配置域名映射

于/etc/nginx/conf.d目錄下,增加配置文件www-dev.integration.enjoyor.net.conf(建議配置文件以域名命名,且一個(gè)配置文件配置一個(gè)域名)

upstream integration {
    server                192.168.20.106:9980 weight=1 max_fails=1 fail_timeout=1s;
}


server {
    listen       80;
    server_name  www-dev.integration.enjoyor.net;

    #charset koi8-r;
    access_log        /var/log/nginx/logs/dev.integration.enjoyor.net.access.log     main;
    error_log         /var/log/nginx/logs/dev.integration.enjoyor.net.error.log      notice;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        proxy_next_upstream http_500 http_502 http_503 http_504 timeout error invalid_header;
        proxy_set_header Host $host;
        proxy_cookie_path /integrationweb/ /;
        proxy_set_header Referer $http_referer;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://integration/integrationweb/;
        
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

   
}

重點(diǎn)
1.配置

 proxy_pass http://integration/integrationweb/;
                   upstream name  / tomcat war包名
 

2.權(quán)限
手動(dòng)創(chuàng)建 日志目錄/var/log/nginx/logs ,nginx沒有權(quán)限

2.報(bào)錯(cuò)與解決

1)權(quán)限問題

Nginx * 1 connect() to 192.168.20.106:9980 failed (13: Permission denied)

此處產(chǎn)生問題的原因是:以root用戶的身份操作nginx。從服務(wù)器運(yùn)維的角度,每一個(gè)工具都應(yīng)該創(chuàng)建其對(duì)應(yīng)的用戶,只有該用戶有權(quán)限操作,若都以root權(quán)限操作會(huì)留下安全隱患(攻擊者可從nginx日志獲取root信息,進(jìn)而操作服務(wù)器)。

但此教程作為測(cè)試便不多做限制,快速達(dá)成效果即可。
解決方案如下:

更改nginx用戶
/etc/nginx/目錄下 nginx.conf 文件 user改為root

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

2)網(wǎng)絡(luò)問題

解決方案如下:

解決方法:

一、關(guān)閉SeLinux

查看SELinux狀態(tài):

1、/usr/sbin/sestatus -v      ##如果SELinux status參數(shù)為enabled即為開啟狀態(tài)

SELinux status:                 enabled

2、getenforce                 ##也可以用這個(gè)命令檢查

關(guān)閉SELinux:

1、臨時(shí)關(guān)閉(不用重啟機(jī)器):

setenforce 0                  ##設(shè)置SELinux 成為permissive模式

##setenforce 1 設(shè)置SELinux 成為enforcing模式

2、修改配置文件需要重啟機(jī)器:

修改/etc/selinux/config 文件

將SELINUX=enforcing改為SELINUX=disabled

重啟機(jī)器即可

二、執(zhí)行下面的命令

setsebool -P httpd_can_network_connect 1

三、hosts增加DNS解析(用于生產(chǎn)環(huán)境)

c:\Windows\System32\Drivers\etc 路徑下 hosts文件增加192.168.20.90(具體DNS服務(wù)器)
會(huì)優(yōu)先以192.168.20.90 配置的 www-dev.integration.enjoyor.net 指向 192.168.20.106的規(guī)則解析域名

cmd 中 ipconfig /flushdns 刷新DNS緩存后生效。

四、hosts添加本地映射(用于開發(fā)環(huán)境)

1.修改hosts文件,讓域名以本地配置的規(guī)則進(jìn)行映射

# localhost name resolution is handled within DNS itself.
#192.168.20.90
127.0.0.1     www-dev.integration.enjoyor.net

2.安裝本地nginx

1)nginx.conf配置
文件末尾添加 include conf.d/*.conf,引入同級(jí)文件夾conf.d下的所有.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;

    sendfile        on;

    keepalive_timeout  65;
   
    include conf.d/*.conf;

}

2)conf.d配置
www-dev.integration.enjoyor.net.conf 文件映射本地tomcat

upstream integration {
    server          127.0.0.1:8081 weight=1 max_fails=1 fail_timeout=1s;
}


server {
    listen       80;
    server_name  www-dev.integration.enjoyor.net;

    location / {

        proxy_next_upstream http_500 http_502 http_503 http_504 timeout error invalid_header;
        proxy_set_header Host $host;
        proxy_cookie_path /integrationweb/ /;
        proxy_set_header Referer $http_referer;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://integration/integrationweb/;
        
    }

}

至此,通過修改hosts文件的域名映射關(guān)系,可以在本地靈活切換開發(fā)和生產(chǎn)環(huán)境的接口,便于后端人員調(diào)試。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容