通過Nginx解決網(wǎng)絡(luò)隔離實(shí)踐記錄詳解
需求
最近需要遷移Node線上服務(wù),于是新申請了兩臺(tái)線上服務(wù)器;
部署服務(wù)器后,需要驗(yàn)證服務(wù)是否正常,辦公環(huán)境與線上環(huán)境網(wǎng)絡(luò)是隔離的,無法直接訪問;但是,線上服務(wù)器可通過部署服務(wù)器訪問,而辦公網(wǎng)絡(luò)是可以訪問部署機(jī)的;
所以,可通過在部署機(jī)上配置代理的方式,辦公環(huán)境請求部署機(jī),然后把請求代理到線上服務(wù)的方式驗(yàn)證服務(wù)是否正常。
整個(gè)網(wǎng)絡(luò)結(jié)構(gòu)如下圖所示:
Nginx安裝
下載
下載頁面: http://nginx.org/en/download.html選擇版本鼠標(biāo)右鍵拷貝鏈接地址
# 下載 [work@40-10-14 opt]$ wget http://nginx.org/download/nginx-1.18.0.tar.gz # 解壓文件 [work@40-10-14 opt]$ tar -xvf nginx-1.18.0.tar.gz
安裝
# 1. 默認(rèn)安裝:root權(quán)限進(jìn)入解壓后的目錄,執(zhí)行如下命令安裝 [root@40-10-14 nginx-1.18.0]# ./configure && make && make install # 2.指定目錄:安裝到指定的/opt/nginx目錄 [work@40-10-14 opt]$ mkdir /opt/nginx [work@40-10-14 nginx-1.18.0]$ ./configure --prefix=/opt/nginx && make && mae install
默認(rèn)安裝,非root權(quán)限會(huì)報(bào)如下錯(cuò)誤
mkdir: cannot create directory `/usr/local/nginx': Permission denied make[1]: *** [install] Error 1 make[1]: Leaving directory `/opt/nginx-1.18.0' make: *** [install] Error 2
默認(rèn)安裝后,查看nginx的安裝目錄,可以看到安裝在/usr/local/nginx目錄下
[root@40-10-14 opt]# whereis nginx nginx: /usr/local/nginx
1.建議使用指定目錄方式安裝。如果切換為root權(quán)限去安裝,后續(xù)修改config文件也需要root權(quán)限
2.或者root安裝后,修改權(quán)限為普通用戶可操作也行
添加軟鏈
添加軟鏈,使得nginx命令全局能訪問,每次運(yùn)行就不用切換到安裝目錄中了
# 添加軟鏈 [root@40-10-14 sbin]# ln -s /opt/nginx/sbin/nginx /usr/local/bin/ # 查看版本 [root@40-10-14 sbin]# nginx -v nginx version: nginx/1.18.0
常用命令
- 啟動(dòng):nginx
- 停止:nginx -s stop
- 重啟:nginx -s reload
- 幫助命令: nginx -h
強(qiáng)制停止:
# 查看linux進(jìn)程id [root@40-10-14 ~]# ps -ef | grep nginx nobody 45198 1 0 16:12 ? 00:00:00 nginx: worker process root 51261 50692 0 17:00 pts/0 00:00:00 grep nginx # 關(guān)閉進(jìn)程 [root@40-10-14 ~]# kill 45198 # 之前的進(jìn)程已被關(guān)閉 [root@40-10-14 ~]# ps -ef | grep nginx root 51277 50692 0 17:00 pts/0 00:00:00 grep nginx
配置代理
配置兩臺(tái)機(jī)器的請求轉(zhuǎn)發(fā),編輯nginx安裝目錄下的nginx/conf/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; # 請求需要轉(zhuǎn)發(fā)到如下兩臺(tái)機(jī)器上,流量平分;指定IP和端口 upstream zpserver { server xx.xx.xx.22:10001; server xx.xx.xx.23:10001; } server { # nginx服務(wù)端口為80 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # /user根路徑的請求才轉(zhuǎn)發(fā) location /user { root html; index index.html index.htm; proxy_pass http://zpserver; } #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 localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
注意:修改完nginx的配置文件后,需要運(yùn)行nginx -s reload才能生效
驗(yàn)證
由于線上服務(wù)很多都是需要登錄的,所以訪問時(shí)需要使用域名訪問,而不能使用IP訪問,因?yàn)閏ookie都是跟域名綁定的
解決這個(gè)問題很簡單,配置本機(jī)host即可
# IP為Nginx服務(wù)器IP xx.xx.xx.14 xxx.daojia.com
通過上述配置,在本機(jī)瀏覽器上請求xxx.daojia.com即可間接通過部署機(jī)上的Nginx訪問到線上服務(wù),以此在內(nèi)網(wǎng)測試服務(wù)是否正確;待服務(wù)無異常后,把線上流量切過來即可。
到此這篇關(guān)于通過Nginx解決網(wǎng)絡(luò)隔離實(shí)踐記錄詳解的文章就介紹到這了,更多相關(guān)Nginx 網(wǎng)絡(luò)隔離內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx proxy_set_header的具體實(shí)現(xiàn)
proxy_set_header?是 Nginx 配置中的一個(gè)重要指令,本文主要介紹了nginx proxy_set_header的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07權(quán)限問題導(dǎo)致Nginx 403 Forbidden錯(cuò)誤的解決方法
這篇文章主要介紹了權(quán)限問題導(dǎo)致Nginx 403 Forbidden錯(cuò)誤的解決方法,本文中導(dǎo)致 403 Forbidden錯(cuò)誤的原因是配置文件中沒有指明一個(gè)用戶,需要的朋友可以參考下2014-08-08Nginx配置PHP的Yii與CakePHP框架的rewrite規(guī)則示例
這篇文章主要介紹了Nginx配置PHP的Yii與CakePHP框架的rewrite規(guī)則示例,是這兩款高人氣框架使用Nginx的關(guān)鍵配置點(diǎn),需要的朋友可以參考下2016-01-01詳解NGINX訪問https跳轉(zhuǎn)到http的解決方法
這篇文章主要介紹了詳解NGINX訪問https跳轉(zhuǎn)到http的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例
這篇文章主要介紹了nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決
本文主要介紹了Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03詳解Keepalived+Nginx實(shí)現(xiàn)高可用(HA)
這篇文章主要介紹了詳解Keepalived+Nginx實(shí)現(xiàn)高可用(HA),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06nginx+redis實(shí)現(xiàn)session共享
這篇文章主要為大家詳細(xì)介紹了nginx+redis實(shí)現(xiàn)session的共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03