Translate

[Nginx] 내부 리다이렉션(Internal Redirection) 사용하여 Redirect(301,302)없이 바로 Response(200) 받을 수 있게 변경하기





이슈

http://localhost/test
위의 URL을 호출 시 아래와 같은 URL로 Redirect 되어버린다.
http://localhost/test/








원하는 것

http://localhost/test
위의 URL을 호출 시 Nginx 에서 내부적으로 Redirect 해서 바로 응답코드 200을 돌려준다.






수정방법


nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        error_log /opt/local/share/nginx/logs/error.log debug;

        location / {
            root   share/nginx/html;
            index  index.html index.htm;
            rewrite ^(.*/test)$ $1/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           share/nginx/html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   share/nginx/html;
    #        index  index.html index.htm;
    #    }
    #}

}



- rewrite 방법을 이용해 redirect 시켜주었다.
- error_log 는 디버깅을 위해 넣어주었다.




위의 설정을 해준뒤 Nginx 를 다음과 같이 재기동 했다.


1004lucifer :~ 1004lucifer$ cd {Nginx_HOME}/sbin
1004lucifer :sbin 1004lucifer$ ./nginx -s reload








결과

다음과 같이 URL 이 바뀌지 않고 처리가 되었다.
(이미 캐시 처리가 되어있어 응답코드가 304로 보여진다.)










추가문제점 및 해결방안

URL 이 'http://localhost/test 와 같이 변경이 되면서 추가적인 문제가 발생을 했다.

1. http://localhost/test/index.html 내부에 있는 리소스(image, css, script) 들이 상대경로로 되어있다.

2. img/a.png 리소스의 이미지는 다음과 같이 브라우저에 요청을 했다.
    - http://localhost/img/a.png


따라서 해당 html 페이지 내부에 있는 img, css, script 리소스를 절대경로로 변경하여 해결을 했다.







참조
http://opentutorials.org/module/384/4337
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite


댓글