Nginx實(shí)現(xiàn)端口映射的示例代碼
1. 找到nginx的部署路徑
[root@SH-DB-02 adi]# ps -ef | grep nginx root 53617 1 0 2019 ? 00:00:07 nginx: master process ./nginx nobody 176294 53617 0 12:31 ? 00:00:01 nginx: worker process [root@SH-DB-02 adi]# [root@SH-DB-02 adi]# [root@SH-DB-02 adi]# [root@SH-DB-02 adi]# ls -la /proc/53617/exe lrwxrwxrwx 1 root root 0 6月 20 14:01 /proc/53617/exe -> /opt/nginx/webserver/nginx/sbin/nginx
最終找到的nginx的路徑為“ /opt/nginx/webserver/nginx/sbin/nginx”
2. 備份原來的配置文件
cd /opt/nginx/webserver/nginx/conf/ cp nginx.conf nginx20250620.txt
3. 編輯nginx.conf文件
nano nginx.conf
4. 在http塊中添加新的server配置
在現(xiàn)有的server塊(8080端口那個)后面,在http塊的最后一個 } 之前添加:
server {
listen 8083;
server_name 本機(jī)IP;
# 只代理特定路徑
location /xizangott/C2callback {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 設(shè)置超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 其他請求返回404
location / {
return 404;
}
}
5. 完整的nginx.conf應(yīng)該是這樣的:
user nobody;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
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"';
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for "$request_time"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
autoindex_exact_size off;
autoindex_localtime on;
autoindex on;
gzip on;
server {
listen 8080;
server_name 本機(jī)IP;
index index.html index.htm;
root /data/pic;
autoindex off;
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location /ngx_status {
stub_status on;
access_log off;
allow 127.0.0.1 ;
deny all;
}
}
server {
listen 8083;
server_name 本機(jī)IP;
# 只代理特定路徑
location /xizangott/C2callback {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 設(shè)置超時時間
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 其他請求返回404
location / {
return 404;
}
}
}
6. 保存文件并退出nano
按 Ctrl + X,然后按 Y,最后按 Enter
7. 測試配置文件語法
cd /opt/nginx/webserver/nginx/sbin/ ./nginx -t
8. 重新加載nginx配置
./nginx -s reload
9. 驗(yàn)證配置是否生效
# 檢查8083端口是否在監(jiān)聽 netstat -tulpn | grep :8083 # 檢查nginx進(jìn)程 ps -ef | grep nginx
10. 測試代理功能
# 測試目標(biāo)路徑 curl -I http://本機(jī)ip:8083/xizangott/C2callback # 測試其他路徑(應(yīng)該返回404) curl -I http://本機(jī)ip:8083/test
11. 查看日志(如果有問題)
# 查看錯誤日志 tail -f /opt/nginx/webserver/nginx/logs/error.log # 查看訪問日志 tail -f /opt/nginx/webserver/nginx/logs/access.log
按照以上步驟操作即可。配置完成后,訪問 http://本機(jī)IP:8083/xizangott/C2callback 就會被代理到你的8081端口的Java應(yīng)用了。
這樣訪問http://本機(jī)ip:8083/xizangott/C2callback會映射到“http://本機(jī)ip:8081/xizangott/C2callback”
到此這篇關(guān)于Nginx實(shí)現(xiàn)端口映射的示例代碼的文章就介紹到這了,更多相關(guān)Nginx 端口映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx日志輪轉(zhuǎn)與定時啟動腳本的實(shí)現(xiàn)
本文主要介紹了Nginx日志輪轉(zhuǎn)與定時啟動腳本的實(shí)現(xiàn),通過批處理腳本rotate-logs.bat實(shí)現(xiàn)日志輪轉(zhuǎn),使用start-nginx.bat啟動和重啟Nginx服務(wù),借助Windows任務(wù)計劃程序每日凌晨自動執(zhí)行日志輪轉(zhuǎn),確保Nginx服務(wù)正常啟動,感興趣的可以了解一下2026-04-04
使用Nginx實(shí)現(xiàn)https請求轉(zhuǎn)發(fā)http實(shí)踐
這篇文章主要介紹了使用Nginx實(shí)現(xiàn)https請求轉(zhuǎn)發(fā)http實(shí)踐,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-03-03
Nginx指令add_header和proxy_set_header的區(qū)別及說明
這篇文章主要介紹了Nginx指令add_header和proxy_set_header的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
通過Nginx(basic auth)實(shí)現(xiàn)Prometheus賬號密碼登錄
本文主要介紹了在RedHat7.5服務(wù)器上安裝和配置Grafana和Prometheus,并通過HTTPD和Nginx實(shí)現(xiàn)Prometheus的賬號密碼認(rèn)證訪問,具有一定的參考價值,感興趣的可以了解一下2026-01-01
詳解Nginx之Location配置(Location匹配順序)
這篇文章主要介紹了詳解Nginx之Location配置(Location匹配順序),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
nginx+uwsgi啟動Django項(xiàng)目的詳細(xì)步驟
nginx+uwsgi+django是我們常用的django部署方式。這篇文章主要介紹了nginx+uwsgi啟動Django項(xiàng)目的詳細(xì)步驟,非常具有實(shí)用價值,需要的朋友可以參考下2018-10-10
nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過程
這篇文章主要介紹了nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過程,2024-06-06

