Jump to content

Setting up SFTP with Nginx

From LemonWiki共筆
Revision as of 13:42, 20 January 2025 by Planetoid (talk | contribs) (Created page with " = Nginx Directory Permissions and Symbolic Link Configuration Guide = == Environment Overview == * Using SFTP to upload files to personal directory * Personal directory linked to website root via symbolic link * Files accessible through public web URL after upload === Example SFTP Upload Process === # Upload file to personal directory: <code>/home/your_user/public_folder/</code> # File automatically reflects in website directory: <code>/usr/share/nginx/html/folder/...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Nginx Directory Permissions and Symbolic Link Configuration Guide

Environment Overview

  • Using SFTP to upload files to personal directory
  • Personal directory linked to website root via symbolic link
  • Files accessible through public web URL after upload

Example SFTP Upload Process

  1. Upload file to personal directory: /home/your_user/public_folder/
  2. File automatically reflects in website directory: /usr/share/nginx/html/folder/
  3. Access via URL: http://your_domain/folder/file.png

Configuration Steps

  1. Verify Symbolic Link

<syntaxhighlight lang="bash"># Check if symbolic link is correct ls -l /home/your_user/public_folder</syntaxhighlight> Expected output:

lrwxrwxrwx 1 your_user your_user 26 Jan 20 15:00 /home/your_user/public_folder -> /usr/share/nginx/html/folder
  1. Check Directory Structure and Permissions

<syntaxhighlight lang="bash"># Check website root directory sudo ls -lht /usr/share/nginx/html/

  1. Check target directory

sudo ls -lht /usr/share/nginx/html/folder/</syntaxhighlight>

  1. Check Nginx Configuration

<syntaxhighlight lang="bash"># Check nginx config sudo nano /etc/nginx/nginx.conf

  1. Or check site config

sudo nano /etc/nginx/sites-available/default</syntaxhighlight> Basic nginx configuration should include:

<syntaxhighlight lang="nginx">server {

   listen 80;
   server_name your_domain;
   root /usr/share/nginx/html;
   index index.html;
   location / {
       try_files $uri $uri/ =404;
   }

}</syntaxhighlight>

Common Issues:

  • Specific directories return 404 (files exist)
  • Files uploaded via SFTP not accessible via web
  • Nginx error log shows permission issues

Solution: Adjust Permission Settings

  1. Check Nginx Error Log

<syntaxhighlight lang="bash">sudo tail /var/log/nginx/error.log</syntaxhighlight> Possible error message:

[crit] stat() "[path]/file.png" failed (13: Permission denied)
  1. Set User Directory Permissions

<syntaxhighlight lang="bash"># Ensure user directory is accessible sudo chmod 755 /home/your_user</syntaxhighlight>

  1. Set Website Directory Permissions

<syntaxhighlight lang="bash"># Ensure website directory is accessible sudo chmod 755 /home/your_user/public_folder</syntaxhighlight>

  1. (optional) Set File Permissions

<syntaxhighlight lang="bash"># Set file read permissions chmod 644 /usr/share/nginx/html/folder/your_file.png</syntaxhighlight>

Permission Details

  • chmod 755 (directories):
    • Owner: read, write, execute (rwx)
    • Group: read and execute (r-x)
    • Others: read and execute (r-x)
  • chmod 644 (files):
    • Owner: read, write (rw-)
    • Group: read (r–)
    • Others: read (r–)

Testing and Verification

  1. Public URL format: http://your_domain/folder/file.png

  2. If still inaccessible after configuration:

    <syntaxhighlight lang="bash"># Reload nginx configuration

    sudo systemctl reload nginx</syntaxhighlight>

Important Notes

  1. Permission changes take effect immediately, no nginx restart needed
  2. Entire path needs proper execute permissions
  3. New files need correct permissions set
  4. Ensure symbolic links remain intact
  5. Verify files are uploaded to correct location