Repository: xmobile-atamyrat/xmobile-v1
Pull request #112: feat/nginx-conf
Author: login: Babahan-0906
Created: 2025-04-25T05:26:09Z | Merged: 2025-04-27T12:54:02Z

### Description
- update nginx config to route `websocket` reqs to localhost:4000
- create script to update and reload nginx conf

### Details
- on nginx config only routing `/ws/` reqs was added (the rest existed on the config):
```
location /ws/ {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "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_read_timeout 3600s;
        proxy_send_timeout 3600s;
}
```
- purpose of: 
```
   proxy_read_timeout 3600s;
   proxy_send_timeout 3600s;
```
we are using nginx as a reverse-proxy, and default time for handling requests is `1minute`. As this is chat feature, the reply between users might be delayed, so timeout time is increased to `1h`


Diff:
diff --git a/nginx/default b/nginx/default
new file mode 100644
index 00000000..01859656
--- /dev/null
+++ b/nginx/default
@@ -0,0 +1,43 @@
+server {
+    listen 80;
+    server_name xmobile.com.tm www.xmobile.com.tm;
+
+    # Redirect all HTTP requests to HTTPS
+    return 301 https://$host$request_uri;
+}
+
+server {
+    listen 443 ssl;
+    server_name xmobile.com.tm www.xmobile.com.tm;
+
+    ssl_certificate /home/ubuntu/certificates/ssl-bundle.crt;
+    ssl_certificate_key /home/ubuntu/certificates/xmobile.com.tm_key.txt;
+
+    access_log /home/ubuntu/scripts/nginx.access.log;
+    error_log /home/ubuntu/scripts/nginx.error.log;
+
+    client_max_body_size 10M;
+
+    location /ws/ {
+        proxy_pass http://localhost:4000;
+        proxy_http_version 1.1;
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header Connection "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_read_timeout 3600s;
+        proxy_send_timeout 3600s;
+    }
+
+    location / {
+        proxy_pass http://localhost:3000;
+        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_set_header Host $host;
+    }
+}
+
diff --git a/scripts/prod_scripts/restart-nginx.sh b/scripts/prod_scripts/restart-nginx.sh
new file mode 100644
index 00000000..8fac97a7
--- /dev/null
+++ b/scripts/prod_scripts/restart-nginx.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+sudo cp /home/ubuntu/xmobile-v1/nginx/default /etc/nginx/sites-available/default
+
+if ! sudo nginx -t; then
+    echo "NGINX config has errors."
+    exit 1
+fi
+
+echo "NGINX config successfully compiled. Reloading service..."
+sudo nginx -s reload
\ No newline at end of file
diff --git a/scripts/prod_scripts/start-server.sh b/scripts/prod_scripts/start-server.sh
index 8959089c..1ab2024c 100755
--- a/scripts/prod_scripts/start-server.sh
+++ b/scripts/prod_scripts/start-server.sh
@@ -1,2 +1,3 @@
 cd /home/ubuntu/xmobile-v1
 yarn start &> /home/ubuntu/scripts/next-server.log &
+yarn start:ws &> /home/ubuntu/scripts/websocket-server.log &
diff --git a/scripts/prod_scripts/stop-server.sh b/scripts/prod_scripts/stop-server.sh
index edad8039..f578452d 100755
--- a/scripts/prod_scripts/stop-server.sh
+++ b/scripts/prod_scripts/stop-server.sh
@@ -1,11 +1,22 @@
-output=$(sudo netstat -tulnpe | grep ':3000')
+output=$(sudo netstat -tulnpe)
+nextserver=$(echo "$output" | grep ':3000')
+websocketserver=$(echo "$output" | grep ':4000')
 
-pid=$(echo "$output" | awk -F'/' '{print $1}' | awk '{print $NF}')
+pid_ns=$(echo "$nextserver" | awk -F'/' '{print $1}' | awk '{print $NF}')
+pid_ws=$(echo "$websocketserver" | awk -F'/' '{print $1}' | awk '{print $NF}')
 
-# Check if we got a PID
-if [[ -z "$pid" ]]; then
+# Check if we got a PID on next server
+if [[ -z "$pid_ns" ]]; then
     echo "No process found listening on port 3000."
 else
-    echo "Killing process $pid on port 3000."
-    sudo kill "$pid"
+    echo "Killing process $pid_ns on port 3000."
+    sudo kill "$pid_ns"
+fi
+
+# Check if we got a PID on websocket-server
+if [[ -z "$pid_ws" ]]; then
+    echo "No process found listening on port 4000."
+else
+    echo "Killing process $pid_ws on port 4000."
+    sudo kill "$pid_ws"
 fi
diff --git a/scripts/prod_scripts/update-server-source.sh b/scripts/prod_scripts/update-server-source.sh
index 400a0f27..636b224f 100755
--- a/scripts/prod_scripts/update-server-source.sh
+++ b/scripts/prod_scripts/update-server-source.sh
@@ -32,6 +32,10 @@ echo "Copying the 'public'"
 rm -rf public
 mv /home/ubuntu/tar-file/app/xmobile-v1/public .
 
+echo "Copying the 'nginx'"
+rm -rf nginx
+mv /home/ubuntu/tar-file/app/xmobile-v1/nginx .
+
 echo "Copying 'package.json'"
 mv /home/ubuntu/tar-file/app/xmobile-v1/package.json .
 
@@ -46,6 +50,7 @@ mv /home/ubuntu/tar-file/app/xmobile-v1/tsconfig.json .
 
 rm -rf .next
 yarn build
+yarn build:ws
 /home/ubuntu/scripts/restart-server.sh
 
 cd /home/ubuntu/tar-file


Reviews:
user: login: Intizar-T
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-27T10:37:13Z
user: login: Babahan-0906
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-27T11:37:43Z
user: login: Intizar-T
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-27T12:17:37Z
user: login: Intizar-T
body: lgtm!
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-27T12:51:55Z

Review comments:
diff_hunk: @@ -0,0 +1,11 @@
+#!/bin/bash
+
+sudo mv /home/ubuntu/xmobile-v1/nginx/default /etc/nginx/sites-available/default
path: scripts/prod_scripts/restart-nginx.sh
user: login: Intizar-T
body: - how about `cp` instead of `mv`? but this is minor, up to you
- you need to update `scripts/prod_scripts/update-server-source.sh` to `cp` `nginx` dir as well and update that script on ubuntu as well
created_at: 2025-04-27T10:37:12Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,11 @@
+#!/bin/bash
+
+sudo mv /home/ubuntu/xmobile-v1/nginx/default /etc/nginx/sites-available/default
path: scripts/prod_scripts/restart-nginx.sh
user: login: Babahan-0906
body: Understood, copying is much better 💪🏿
created_at: 2025-04-27T11:37:42Z
author_association: COLLABORATOR
path: scripts/prod_scripts/update-server-source.sh
user: login: Intizar-T
body: you also need to `mv` the nginx dir
created_at: 2025-04-27T12:17:37Z
author_association: CONTRIBUTOR

PR comments:
user: login: Intizar-T
created_at: 2025-04-27T10:39:12Z
body: lets update `start-server.sh` and `stop-server.sh` scripts in this PR to start and stop the `ws` server along with the main server
author_association: CONTRIBUTOR
user: login: Babahan-0906
created_at: 2025-04-27T11:39:27Z
body: > lets update `start-server.sh` and `stop-server.sh` scripts in this PR to start and stop the `ws` server along with the main server

Roger that, will be pushing them in minutes 
author_association: COLLABORATOR