Last active
May 30, 2024 14:35
-
-
Save majudhu/d6b42c2551a4d99c74282c7d4c2e3777 to your computer and use it in GitHub Desktop.
nginx with nextjs and expressjs api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proxy_cache_path /var/cache/nginx keys_zone=my_cache:20m use_temp_path=off; | |
proxy_cache my_cache; | |
proxy_cache_lock on; | |
proxy_cache_lock_timeout 20s; | |
map $http_authorization $cachecontrol { | |
"" "public, max-age=10, s-maxage=5, stale-while-revalidate=20"; | |
} | |
server { | |
listen 80 default_server; | |
server_name _; | |
client_max_body_size 100M; # allow huge uploads, upto 100MB | |
root /home/user/app/public; # serve static files from public dir with nginx | |
try_files $uri @next; # serve static files with nginx, fallback to nextjs on not found | |
#add_header X-Cache-Status $upstream_cache_status; # debug, do not enable on production | |
location @next { | |
include proxy_params; | |
proxy_pass http://127.0.0.1:3000; | |
} | |
location /api { | |
include proxy_params; | |
proxy_pass http://127.0.0.1:3001; | |
#add_header X-Cache-Status $upstream_cache_status; | |
#proxy_cache_valid 15s; # if no cache-control headers, cache 200,301,302 responses | |
#proxy_cache_bypass $http_authorization; # either bypass cache | |
#add_header Cache-Control $cachecontrol; | |
#proxy_set_header Authorization ""; # or make requests as public | |
#add_header Cache-Control "public, max-age=10, s-maxage=5, stale-while-revalidate=20"; | |
} | |
location /images/ { | |
root /home/user/app/public/; # will serve files in home/user/app/public/images | |
} | |
listen [::]:443 ssl; # managed by Certbot | |
listen 443 ssl; # managed by Certbot | |
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot | |
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot | |
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot | |
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot | |
} | |
server { | |
if ($host = example.com) { | |
return 301 https://example.com$request_uri; | |
} # managed by Certbot | |
listen 80; | |
listen [::]:80; | |
server_name example.com; | |
return 404; # managed by Certbot | |
} | |
server { | |
server_name www.example.com; | |
return 301 https://example.com$request_uri; | |
} | |
server { | |
server_name _; | |
listen 80 default_server; | |
return 301 https://example.com$request_uri; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# start the nextjs app in pm2, with custom port number | |
pm2 start --name example.com npm -- run start -- -p 3002 | |
# start the expressjs app in pm2 | |
pm2 start --name example.com-api index.js | |
# save the current pm2 list to auto-start | |
pm2 save | |
# to install pm2 as a system service run `pm2 startup` |
Author
majudhu
commented
Jan 26, 2022
location /app {
include proxy_params;
proxy_pass http://127.0.0.1:3000;
}
location /app/api/ {
include proxy_params;
proxy_pass http://127.0.0.1:3001/api/;
}
location /app/images/ {
alias /home/apps/app-api/public/images/;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment