nginx에서 rewrite 사용하기? 채택완료
안녕하세요. @ignaz
centos에서 apache 쓰다가 nginx로 넘어갔습니다.
http://sir.kr/g5_tip/4022">http://sir.kr/g5_tip/4022 'ignaz'님 짧은 주소 팁 설정을 하려고 하는데 잘 안되서요.
nginx.conf에서 http { ... } 사이에 아래 코드를 넣었고요..
적용 방법에 아래처럼 수정하라고 되있어서..
</p><p># rewrite 적용하기</p><p>include rewrite.conf;</p><p> </p><p>location ~ \.php$ {</p><p> root html;</p><p> fastcgi_pass 127.0.0.1:9000;</p><p> fastcgi_index index.php;</p><p> fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span></p><p> # shorturl 환경변수</p><p> fastcgi_param G5_SHORTURL_USE $G5_SHORTURL_USE;</p><p> fastcgi_param G5_SHORTURL_BOARD_ONLY $G5_SHORTURL_BOARD_ONLY;</p><p> fastcgi_param G5_SHORTURL_BOARD_NAME $G5_SHORTURL_BOARD_NAME;</p><p> fastcgi_param G5_SHORTURL_BOARD_EXCLUDE $G5_SHORTURL_BOARD_EXCLUDE;</p><p> include fastcgi_params;</p><p>}</p><p>
rewrite.conf 파일도 nginx 폴더에 넣었는데 위 코드를 nginx.conf에 넣고 nginx을 restart 하면
[root@taemin-sv nginx]# service nginx restart
nginx: [emerg] "set" directive is not allowed here in /etc/nginx/rewrite.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
라고 에러가 자꾸 뜹니다.. 5번째 줄에 set $G5_SHORTURL_USE true; 라고 적혀있는데 뭐가 문제인지 모르겠네요 ㅠㅠ
답변 4개
첨부된 파일에 설명이 조금 미흡했나봐요 ^^;
에러를 확인해보니 "set" 지시어는 여기에 허용하지 않는다라고 하네요.
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#set">http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#set
| Syntax: | set
|
|---|---|
| Default: | — |
| Context: | server, location, if
|
server, location, if 에서만 사용 가능하므로
http { ... } 사이에 코드를 넣었다면 오류가 나는게 맞습니다.
</p><p><span style="font-size: 14.6667px;">http {</span></p><p><span style="font-size: 14.6667px;"> include mime.types;</span></p><p><span style="font-size: 14.6667px;"> default_type application/octet-stream;</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> ....중략....</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> server {</span></p><p><span style="font-size: 14.6667px;"> listen 80;</span></p><p><span style="font-size: 14.6667px;"> server_name <a href="<a href="http://www.mydomain.com;" target="_blank" rel="noopener noreferrer">http://www.mydomain.com;</a>">www.mydomain.com;</a></span></p><p><span style="font-size: 14.6667px;"> root /home/public_html;</span></p><p><span style="font-size: 14.6667px;"> index index.php;</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> ....중략 ....</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> # rewrite 적용하기</span></p><p><span style="font-size: 14.6667px;"> include rewrite.conf;</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> location ~ \.php$ { </span></p><p><span style="font-size: 14.6667px;"> root html;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_pass 127.0.0.1:9000;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_index index.php;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;</span></p><p><span style="font-size: 14.6667px;"> </span></p><p><span style="font-size: 14.6667px;"> # shorturl 환경변수</span></p><p><span style="font-size: 14.6667px;"> fastcgi_param G5_SHORTURL_USE $G5_SHORTURL_USE;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_param G5_SHORTURL_BOARD_ONLY $G5_SHORTURL_BOARD_ONLY;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_param G5_SHORTURL_BOARD_NAME $G5_SHORTURL_BOARD_NAME;</span></p><p><span style="font-size: 14.6667px;"> fastcgi_param G5_SHORTURL_BOARD_EXCLUDE $G5_SHORTURL_BOARD_EXCLUDE;</span></p><p><span style="font-size: 14.6667px;"> include fastcgi_params;</span></p><p><span style="font-size: 14.6667px;"> }</span></p><p><span style="font-size: 14.6667px;"> }</span></p><p><span style="font-size: 14.6667px;">}</span></p><p><span style="font-size: 14.6667px;">
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
사용중인 서버의 디폴트 파일이랑 조금 다르지만 확인해보니
centos에서 nginx 설치한 경우 이처럼 나오는거 같네요.
해당 폴더에 파일이 없다면 아래 코드로 파일 생성 후 적용해보세요.
cat > /etc/nginx/sites-available/default
server {
listen 80 default_server;
server_name _;
root /srv/www/default;
location / {
index index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /srv/www/default;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/www/default;
}
}
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
</p><p>user nginx;</p><p>worker_processes 1;</p><p> </p><p>error_log /var/log/nginx/error.log warn;</p><p>pid /var/run/nginx.pid;</p><p> </p><p> </p><p>events {</p><p> worker_connections 1024;</p><p>}</p><p> </p><p> </p><p>http {</p><p> include /etc/nginx/mime.types;</p><p> default_type application/octet-stream;</p><p> </p><p> log_format main '$remote_addr - $remote_user [$time_local] "$request" '</p><p> '$status $body_bytes_sent "$http_referer" '</p><p> '"$http_user_agent" "$http_x_forwarded_for"';</p><p> </p><p> access_log /var/log/nginx/access.log main;</p><p> </p><p> sendfile on;</p><p> #tcp_nopush on;</p><p> </p><p> keepalive_timeout 65;</p><p> </p><p> #gzip on;</p><p> </p><p> include /etc/nginx/conf.d/*.conf;</p><p> include /etc/nginx/sites-enabled/*.conf; #추가..</p><p>
이게.. nginx.conf default 입니다..
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
nginx.conf에서는 include sites-enabled/* 되있고요.
위에 코드처럼 중간에 include rewrite.conf 넣고 location ~ \.php$ { ... } 안에 shorturl 환경변수 내용 넣었는데 위 처럼 적용하고 nginx 재시작하면 404 Not Found가 떠버립니다..
추가한 코드 제거하면 다시 원래대로 되고요.. 뭐가 문제인지 모르겠네요..