İçeriğe geç
LexerLangv0.1.0

nginx ve systemd

Ters proxy, içerik türü düzeltmeleri, sertleştirilmiş servis birimi ve dağıtım.

Uygulama 127.0.0.1 üzerinde bir portta çalışır; TLS, güvenlik başlıkları ve statik dosyalar nginx'tedir.

systemd birimi#

.env
[Unit]
Description=LexerLang Belgeleri (LexerLang)
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
WorkingDirectory=/var/www/lexer
Environment=HOME=/var/www/lexer
ExecStart=/usr/local/bin/lexer serve /var/www/lexer/app.lexer --production
Restart=always
RestartSec=3

StandardOutput=journal
StandardError=journal
SyslogIdentifier=lexersite

NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/www/lexer
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictSUIDSGID=true

[Install]
WantedBy=multi-user.target

WorkingDirectory önemli: path.pwd bu dizini gösterir ve md-repo, public gibi göreceli yollar buradan çözülür.

Terminal
systemctl daemon-reload
systemctl enable --now lexersite
systemctl status lexersite
journalctl -u lexersite -f

nginx#

nginx
upstream lexersite_app {
    server 127.0.0.1:3500;
    keepalive 16;
}

server {
    listen 443 ssl;
    http2 on;
    server_name lexer.gurerlabs.com;

    ssl_certificate     /etc/letsencrypt/live/lexer.gurerlabs.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/lexer.gurerlabs.com/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options    "nosniff" always;
    add_header Referrer-Policy           "strict-origin-when-cross-origin" always;

    location / {
        proxy_pass http://lexersite_app;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        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 30s;
    }
}

İçerik türü düzeltmesi#

$res.send() Content-Type'ı her koşulda text/html yapıyor. XML ve düz metin dönen adresler için türü nginx düzeltir:

nginx
location = /sitemap.xml {
    proxy_pass http://lexersite_app;
    proxy_set_header Host $host;
    proxy_hide_header Content-Type;
    add_header Content-Type "application/xml; charset=utf-8" always;
    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header X-Content-Type-Options "nosniff" always;
}

proxy_hide_header üst akıştan geleni siler, add_header doğrusunu koyar. Sıra bu ikisinin birlikte yazılmasını gerektirir; yalnızca add_header yazmak iki Content-Type başlığı üretir.

Statik dosyalar#

nginx
location /public/ {
    alias /var/www/lexer/public/;
    access_log off;
    expires 1h;

    gzip on;
    gzip_vary on;
    gzip_min_length 512;
    gzip_types text/css application/javascript image/svg+xml;

    add_header Cache-Control "public, must-revalidate";
    try_files $uri =404;
}

nginx'in global gzip'i genellikle kapalıdır; CSS ve JS diskten sıkıştırılmadan çıkar. Yalnızca bu blokta açmak yeterlidir.

Kökten istenen dosyalar#

nginx
location = /favicon.ico {
    alias /var/www/lexer/public/favicon/favicon.ico;
    access_log off;
    expires 7d;
}

location = /site.webmanifest {
    alias /var/www/lexer/public/site.webmanifest;
    default_type application/manifest+json;
    expires 1d;
}

default_type gerekli: nginx .webmanifest uzantısını tanımıyor ve dosyayı application/octet-stream olarak gönderiyor.

Sertifika#

Terminal
certbot certonly --webroot -w /var/www/html -d lexer.gurerlabs.com

--nginx yerine --webroot: --nginx eklentisi conf dosyalarını kendisi düzenler ve elle yazılmış blokları bozabilir.

Dağıtım#

Terminal
rsync -az --delete \
    --exclude='.lexer/' --exclude='.logs/' \
    ./ sunucu:/var/www/lexer/

ssh sunucu systemctl restart lexersite

.lexer/ ve .logs/ dışarıda bırakılmalı: ilki sunucuda üretilen önbellek dizini, ikincisi çalışma kayıtları. --delete ile birlikte listelenmeselerdi her dağıtım ikisini de silerdi.

Sırada#

Belgeler burada bitiyor. Eksik bulduğunuz bir şey varsa lexerlang@gurerlabs.com.