nginx에서 rewrite 사용하기? 채택완료

안녕하세요. @ignaz 

centos에서 apache 쓰다가 nginx로 넘어갔습니다.

http://sir.kr/g5_tip/4022 'ignaz'님 짧은 주소 팁 설정을 하려고 하는데 잘 안되서요. 

 

nginx.conf에서  http { ... } 사이에 아래 코드를 넣었고요.. 

적용 방법에 아래처럼 수정하라고 되있어서..

Copy
# rewrite 적용하기include rewrite.conf; location ~ \.php$ {    root           html;    fastcgi_pass   127.0.0.1:9000;    fastcgi_index  index.php;    fastcgi_param  SCRIPT_FILENAME  html$fastcgi_script_name;	    # shorturl 환경변수    fastcgi_param  G5_SHORTURL_USE            $G5_SHORTURL_USE;    fastcgi_param  G5_SHORTURL_BOARD_ONLY     $G5_SHORTURL_BOARD_ONLY;    fastcgi_param  G5_SHORTURL_BOARD_NAME     $G5_SHORTURL_BOARD_NAME;    fastcgi_param  G5_SHORTURL_BOARD_EXCLUDE  $G5_SHORTURL_BOARD_EXCLUDE;    include        fastcgi_params;}

 

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개

채택된 답변
+20 포인트

첨부된 파일에 설명이 조금 미흡했나봐요 ^^;

에러를 확인해보니 "set" 지시어는 여기에 허용하지 않는다라고 하네요.

 

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#set 

 

Syntax:set $variable value;
Default:
Context:serverlocationif

 

server, location, if 에서만 사용 가능하므로 

http { ... } 사이에 코드를 넣었다면 오류가 나는게 맞습니다.

 

Copy
http {    include       mime.types;    default_type  application/octet-stream;     ....중략....     server {        listen      80;        server_name  www.mydomain.com;        root /home/public_html;        index index.php;         ....중략 ....         # rewrite 적용하기        include rewrite.conf;         location ~ \.php$ {             root           html;            fastcgi_pass   127.0.0.1:9000;            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  html$fastcgi_script_name;                        # shorturl 환경변수            fastcgi_param  G5_SHORTURL_USE            $G5_SHORTURL_USE;            fastcgi_param  G5_SHORTURL_BOARD_ONLY     $G5_SHORTURL_BOARD_ONLY;            fastcgi_param  G5_SHORTURL_BOARD_NAME     $G5_SHORTURL_BOARD_NAME;            fastcgi_param  G5_SHORTURL_BOARD_EXCLUDE  $G5_SHORTURL_BOARD_EXCLUDE;            include        fastcgi_params;        }    }}
로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

nginx.conf에는 server { } 가 없고 sites-enabled 폴더에 conf 파일을 따로 만들었습니다.
nginx.conf에서는 include sites-enabled/* 되있고요.
위에 코드처럼 중간에 include rewrite.conf 넣고 location ~ \.php$ { ... } 안에 shorturl 환경변수 내용 넣었는데 위 처럼 적용하고 nginx 재시작하면 404 Not Found가 떠버립니다..
추가한 코드 제거하면 다시 원래대로 되고요.. 뭐가 문제인지 모르겠네요..

댓글을 작성하려면 로그인이 필요합니다.

사용중인 서버의 디폴트 파일이랑 조금 다르지만 확인해보니

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개

다시 해봐야겠네요.. 늦은 시간인데 감사합니다.

댓글을 작성하려면 로그인이 필요합니다.

Copy
user  nginx;worker_processes  1; error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;  events {    worker_connections  1024;}  http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;     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;     keepalive_timeout  65;     #gzip  on;     include /etc/nginx/conf.d/*.conf;    include /etc/nginx/sites-enabled/*.conf; #추가..

 

이게.. nginx.conf default 입니다..

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

server {}가 없을수가 없는데.. nginx.conf.default 파일로 다시 작업해보실래요?

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고