NGINX & Jenkins & Node-RED on Windows

RuzeriE K.
5 min readFeb 4, 2021

--

ลองใช้ nginx ทำ reverse proxy ของ jenkins กับ node-red ซะหน่อย เนื่องจากน้องที่อยู่ทีมเดียวกัน เห็นว่า ip มันไม่ไฮ เขาก็เลยสอนการใช้ nginx แบบผิวๆ ให้

ฉะนั้นในบทความนี้ จะไม่เจอ

  1. การสร้าง domain name (ใบ้ให้นิดว่าใช้ cloudns 😁)
  2. คำอธิบายว่า nginx, jenkins และ node-red คืออะไร ใช้ทำอะไร
  3. วิธีติดตั้ง jenkins และ node-red

#1 ติดตั้ง NGINX บน Windows กันก่อน

ไปที่ https://nginx.org/en/download.html เพื่อดาวน์โหลดไฟล์มาติดตั้ง โดยให้เลือกอันที่เป็นstable version for Windows

donwload nginx for windows

จะได้เป็น zip ไฟล์มา ก็ extract ไฟล์ปกติ เลือกว่าจะวางที่ไหน หน้าตาที่ได้จะประมาณนี้

nginx files

ซึ่งคำสั่งที่จะให้ nginx ทำงาน จะถูกเก็บไว้ใน nginx_folder/conf/nginx.conf ลองเปิดขึ้นมาดูหน่อย

nginx config

จะเห็นว่ามัน setting ไว้เยอะมาก เราเองก็ไม่ได้ใช้ขนาดนั้น ให้เอาที่ comment ด้วย # ออกให้หมด มันจะเหลือประมาณนี้

worker_processes  1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;

location / {
root html;
index index.html index.htm;
}

}
}

จากนั้นก็ทดสอบรัน nginx หน่อย ว่าใช้งานได้หรือป่าว โดยบันทึกแล้วก็ลองรันดู ด้วย command linenginx_folder/nginx.exe

แล้วก็ลองเปิด browser มาเช็คดูว่ามันรันจริงหรือป่าว ด้วยlocalhost

ok nginx ทำงานได้, ไปกันต่อ ^^

อธิบายกันนิสสส

ส่วนสำคัญจะอยู่ใน server ภายใต้ http คำส่ง listen กะ server_name จะระบุ domain ใหม่ที่เราต้องการ ส่วน domain เก่า จะอยู่ใน location เดี๋ยวลอง map jenkins จะเห็นภาพมากกว่านี้

#2 มา reverse proxy ให้ Jenkins กัน

หลักการมันก็แค่ไปเซ็ต nginx config ให้จับ domain ใหม่ มาที่ local jenkins server แค่นั้น ซึ่ง config พวกเนี้ย มันก็มีผู้ใหญ่ใจดีทำไว้ให้อยู่แล้ว เราก็ค้นๆ มาใช้ได้ ผมก็ได้จาก ที่นี่ มา

copy config มา จากนั้นก็นำมาวางไว้ใน http ให้อยู่ใต้ server เดิม ประมาณนี้

http {
...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 80; # Listen on port 80 for IPv4 requests
server_name your.new.jenkins.domain;

ignore_invalid_headers off;

location / {
sendfile off;
proxy_pass http://127.0.0.1:8080;
proxy_redirect default;
proxy_http_version 1.1;

# Required for Jenkins websocket agents
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;

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;
proxy_max_temp_file_size 0;

#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off; # Required for HTTP CLI commands
proxy_set_header Connection ""; # Clear for keepalive
}
}

ในนี้ให้ระบุ domain ใหม่ที่เราต้องการตรง server_name และตรง proxy_pass ภายใต้ location จะเป็น domain ของ jenkins server ในเครื่องเรา ซึ่งที่เห็นจะเป็น default domain

บันทึกไฟล์ config แล้วก็ลองรัน nginx ดู จากนั้นก็ลองเรียกด้วย browser ด้วย domain ใหม่ที่เราตั้งไป

nginx บน windows มันจะมีปัญหานิดหน่อย ตรงที่เวลาเราสั่งรันใหม่ มันจะไม่ปิดตัวเก่าให้ เราต้องไปปิดเองใน task manager

end task nginx.exe ตัวเก่าให้หมด แล้วค่อยรันใหม่

อีกเรื่อง หากรันแล้วเจอ error ว่าชื่อ domain ใหม่ที่ระบุนั้นยาวไป ให้แก้โดยเพิ่ม config นี้ ภายใต้ http ไว้บรรทัดบนสุด server_names_hash_bucket_size 64;

#3 มาต่อกันที่ Node-RED

ทำเหมือนกันคือไปหา config มา ลองดู ที่นี่ ก็ได้ ถ้าไม่อยากหาเอง

แล้วก็ copy มาใส่ไว้ใน http ใต้ server เดิม ประมาณนี้

#proxy for node-red @ port :1880
server {
listen 80;
listen [::]:80;

server_name your.new.node-red.domain;

location = /robots.txt {
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /\n";
}
location / {
proxy_pass http://127.0.0.1:1880;

#Defines the HTTP protocol version for proxying
#by default it it set to 1.0.
#For Websockets and keepalive connections you need to use the version 1.1
proxy_http_version 1.1;

#Sets conditions under which the response will not be taken from a cache.
proxy_cache_bypass $http_upgrade;

#These header fields are required if your application is using Websockets
proxy_set_header Upgrade $http_upgrade;

#These header fields are required if your application is using Websockets
proxy_set_header Connection "upgrade";

#The $host variable in the following order of precedence contains:
#hostname from the request line, or hostname from the Host request header field
#or the server name matching a request.
proxy_set_header Host $host;

#Forwards the real visitor remote IP address to the proxied server
proxy_set_header X-Real-IP $remote_addr;

#A list containing the IP addresses of every server the client has been proxied through
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#When used inside an HTTPS server block, each HTTP response from the proxied server is rewritten to HTTPS.
proxy_set_header X-Forwarded-Proto $scheme;

#Defines the original host requested by the client.
proxy_set_header X-Forwarded-Host $host;

#Defines the original port requested by the client.
proxy_set_header X-Forwarded-Port $server_port;

}
}

บันทึก config ปิด nginx เดิม แล้วก็รันใหม่ จากนั้นก็ทดสอบด้วย browser ตามเดิม

ครบแล้ว setting nginx ง่ายนิดเดียว (เพราะมีคนอื่นทำไว้ให้แล้ว 😝)

#4 ตั้ง NGINX กับ Node-RED ให้เป็น Windows Service

แถมหน่อย เนื่องจาก nginx กับ node-red เวลาจะสั่งให้ทำงานเราต้องไปรัน exe ขึ้นมา เวลาที่ไฟดับ restart เครื่องใหม่ เราก็ต้องไปสั่งให้มันรันแบบ manual ซึ่งเป็นปัญหากับชีวิตชาว DevOps อย่างเราๆ ก็เลยต้องเปลี่ยนให้มันเป็น windows service ซะ

อันนี้เราใช้ tool สำหรับ windows ที่ชื่อ nssm

เลือก version pre-release เพราะมันล่าสุดละ 2017–04–26 โน่นแน๊ะ

ดาวน์โหลดมาจะเป็น zip ข้างในจะมี nssm.exe เวลาจะใช้เราก็ต้องสั่งผ่าน command line ด้วย

nssm install <servicename>

แล้วก็จะได้หน้าตาโปรแกรมประมาณนี้มา

  • ระบุ service name ที่ต้องการในช่องด้านล่าง
  • ให้ตั้งค่า nginx.exe และ node-red.exe path ที่ tab Application
  • สำหรับ path ของ node-red ให้ดูจาก ลิงค์ นี้
  • เลื่อน tab หา tab I/O ใน tab นี้ให้เราสร้างไฟล์ io.txt ขึ้นมา รับ response text จากการรัน service โดยแต่ละ service ก็แยกไฟล์ของใครของมัน
  • ตั้งค่าเรียบร้อยแล้ว ก็คลิก Install service แล้วก็ไปหาใน task manager ด้วยชื่อที่เราตั้งไป จากนั้นก็ start service ดังกล่าวขึ้นมา (หากหาไม่เจอ ให้ปิด task manager แล้วเปิดใหม่)

ครบละครับ หากมีผู้ผ่านมาพบ แล้วเจอข้อผิดพลาด แจ้งกันได้ครับ เพราะผมลองไปแค่ผิวๆ จริงๆ เอาพอให้มันทำงานได้ บายยย (^^)/

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

RuzeriE K.
RuzeriE K.

Written by RuzeriE K.

Like to be a Agriculturist and career is Developer around 10+ years but I feel like, I'm still a novice.

No responses yet

Write a response