CentOS7 서버의 특정 디렉토리를 nginx 이용해서 공유할 수 있다.
리눅스 사용을 할 줄 모르는 동료가 있는데, CentOS 서버에 있는 파일을 공유해야할 일이 있을 때 유용하다.
파일이 필요한 동료는 리눅스 서버와 동일 네트워크에 접속한 상태에서 리눅스 서버ip와 nginx 포트만 알면 웹브라우저로 접속해 파일을 다운받을 수 있다.
1. CentOS7에 nginx 설치
sudo yum install nginx
2. nginx 설정 바꾸기
CentOS7에서 설정파일
/etc/nginx/nginx.conf
파일 내용 중 server 부분 내용만 살짝 바꿔주면 된다. 아래는 중요 내용
listen : 80 # 접속할때 사용할 포트. 80을 그대로 써도 되고 사용하고 싶은 포트로 변경해서 사용해도 된다.
root /home/jihoon/nginx-test/; # 공유할 디렉토리 위치
location / {
autoindex on; # autoindex로 브라우저에서 공유 대상 디렉토리 보이기
autoindex_exact_size on; # 파일크기 보여주기
autoindex_format html; # index목록이 출력되어야 하는 형식. html, xml, json, or jsonp 네가지 가능.
}
아래는 예시 파일 내용. 불필요한 내용과 주석을 삭제한 상태이다.
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 88;
server_name localhost;
root /home/lunit/nginx-test/;
location / {
autoindex on; # autoindex로 브라우저에서 공유 대상 디렉토리 보이기
autoindex_exact_size on; # 파일크기 보여주기
autoindex_format html; # index목록이 출력되어야 하는 형식. html, xml, json, or jsonp 네가지 가능.
}
}
}
접속성공하면 본 게시글 상단 이미지와 같다.
접속시 403 Forbidden 에러 떳을때
1. 디렉토리 위치 제대로 쓴거 맞는지 확인
2. 공유할 디렉토리, 파일의 권한 문제
- 공유할 폴더가 /home/jihoon/nginx-text/ 라면 home, jihoon, nginx-test, 그 하위 디렉토리 모두 755 권한이 필요하다. - 공유할 파일은 644권한이 최소. - 아래 명령어로 해결하자.
sudo chmod 755 {dir}
sudo chmod 644 {files}
참고웹 https://linuxhint.com/fix-nginx-403-forbidden/
아이디, 비밀번호 설정해놓고 해당 계정정보 아는 사람만들어오게 하기
아래 링크 따라 하면된다. nginx 서버에 아이디, 비밀번호를 하나 또는 여러개 만들어 놓고 서버에 접속하는 사람들은 그 아이디, 비밀번호를 통해 접속하게 하는 방법이다.
https://ko.linux-console.net/?p=2300
0 댓글