Announcement

Collapse
No announcement yet.

How to move apache rewrites into nginx

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to move apache rewrites into nginx

    If you want to get rid of apache and have your site working purely on nginx, please consider these steps to migrate apache-specific configurations into nginx.

    NOTE: we do not support this officially. Apache + mod_rewrite will still be required for KVS and all functionality we add or change in KVS is always tested in apache environment. We cannot guarantee these steps are everything you need to migrate to pure nginx. Also we cannot guarantee that any future features will work in pure nginx environment or will not require changes in nginx configuration to make them working correctly.

    1) Nginx should set the following env variables which are typically set by apache:
    • REMOTE_ADDR
    • SCRIPT_FILENAME
    • SCRIPT_NAME
    • REQUEST_METHOD
    • QUERY_STRING
    • REQUEST_URI
    • DOCUMENT_ROOT

    These are typically set by default fastcgi configuration, but worth mentioning.

    2) Here is sample configuration to ensure redirect to either www.domain.com (please note that admin panel is excluded, as it can't work under www for historical reasons), or to domain.com.

    For www.domain.com:
    Code:
    server {
            listen       80;
    
            server_name  domain.com www.domain.com;
    
            set $redirect "";
            if ($http_host = domain.com) {
                    set $redirect "1";
            }
            if ($request_uri !~ "^/admin*") {
                    set $redirect "${redirect}1";
            }
            if ($redirect = 11) {
                    return 301 $scheme://www.domain.com$request_uri;
            }
    
            ... config ...
    }
    For domain.com:
    Code:
    server {
            listen       80;
    
            server_name  domain.com www.domain.com;
    
            if ($http_host = www.domain.com) {
                    return 301 $scheme://domain.com$request_uri;
            }
    
            ... config ...
    }
    3) Add the following code into main domain nginx config under location / block:
    Code:
    location ~* \.(eot|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
    }
    This code is needed to correctly display player icons in some browsers.

    4) Add the following rewrite rules into main domain nginx config under server block:
    Code:
    # admin panel rewrites
    rewrite ^/admin/posts_for_(.*)\.php$ /admin/posts_for_types.php?post_type_external_id=$1 last;
    rewrite ^/admin/feeds/(.*)/$ /admin/feeds/get_feed.php?external_id=$1 last;
    rewrite ^/admin/api/c07e0f82365c5f94f3f221a12778d1b6\.php$ /admin/api/kvs_api.php last;
    rewrite ^/admin/billings/([^/]*)/c07e0f82365c5f94f3f221a12778d1b6\.php$ /admin/billings/$1/$1.php last;
    NOTE: you should replace c07e0f82365c5f94f3f221a12778d1b6 with your private key, it is randomly generated for each project. In order to find its value for your project please go to KVS admin panel Administration -> Installation info and look for $config.billing_scripts_name option. You should use your value here.

    5) For the KVS default theme version 7.5 and above add the following rewrite rules into main domain nginx config under server block:

    Code:
    # global ---------------------------------------------------------------------------------------------------------------
    
    rewrite ^/terms/$            /terms.php last;
    rewrite ^/dmca/$             /dmca.php last;
    rewrite ^/2257/$             /2257.php last;
    
    rewrite ^/search/([^/]+)/$   /search.php?q=$1 last;
    rewrite ^/search/$           /search.php last;
    
    rewrite ^/link/([^/]+)/$     /redirect_cs.php?dir=$1 last;
    
    rewrite ^/random_video(/)?$  /redirect_random_video.php last;
    rewrite ^/random_album(/)?$  /redirect_random_album.php last;
    
    # posts example --------------------------------------------------------------------------------------------------------
    
    rewrite ^/news/$                     /posts_list.php?post_type=news last;
    rewrite ^/news/([0-9]+)/$            /posts_list.php?post_type=news&from=$1 last;
    rewrite ^/news/([0-9]+)/([^/]+)/$    /view_post.php?id=$1&dir=$2 last;
    rewrite ^/news/([^/]+)/$             /view_post.php?dir=$1 last;
    
    # videos ---------------------------------------------------------------------------------------------------------------
    
    rewrite ^/latest-updates/([0-9]+)/$      /videos_list_latest.php?from=$1 last;
    rewrite ^/latest-updates/$               /videos_list_latest.php last;
    
    rewrite ^/top-rated/([0-9]+)/$           /videos_list.php?sort_by=rating&from=$1 last;
    rewrite ^/top-rated/$                    /videos_list.php?sort_by=rating last;
    
    rewrite ^/most-popular/([0-9]+)/$        /videos_list.php?sort_by=video_viewed&from=$1 last;
    rewrite ^/most-popular/$                 /videos_list.php?sort_by=video_viewed last;
    
    rewrite ^/categories/([0-9]+)/$          /categories_videos.php?from=$1 last;
    rewrite ^/categories/$                   /categories_videos.php last;
    rewrite ^/categories/([^/]+)/([0-9]+)/$  /videos_list.php?category=$1&from=$2 last;
    rewrite ^/categories/([^/]+)/$           /videos_list.php?category=$1 last;
    
    rewrite ^/sites/([0-9]+)/$               /sponsors_videos.php?from=$1 last;
    rewrite ^/sites/$                        /sponsors_videos.php last;
    rewrite ^/sites/([^/]+)/([0-9]+)/$       /videos_list.php?cs=$1&from=$2 last;
    rewrite ^/sites/([^/]+)/$                /videos_list.php?cs=$1 last;
    
    rewrite ^/models/([0-9]+)/$              /models_videos.php?from=$1 last;
    rewrite ^/models/$                       /models_videos.php last;
    rewrite ^/models/([^/]+)/([0-9]+)/$      /videos_list.php?model=$1&from=$2 last;
    rewrite ^/models/([^/]+)/$               /videos_list.php?model=$1 last;
    
    rewrite ^/tags/$                         /tags_videos.php last;
    rewrite ^/tags/([^/]+)/([0-9]+)/$        /videos_list.php?tag=$1&from=$2 last;
    rewrite ^/tags/([^/]+)/$                 /videos_list.php?tag=$1 last;
    
    rewrite ^/channels/([0-9]+)/$            /channels.php?from=$1 last;
    rewrite ^/channels/$                     /channels.php last;
    rewrite ^/channels/([^/]+)/([0-9]+)/$    /videos_list.php?dvd=$1&from=$2 last;
    rewrite ^/channels/([^/]+)/$             /videos_list.php?dvd=$1 last;
    
    rewrite ^/private/([0-9]+)/$             /videos_list.php?is_private=1&from=$1 last;
    rewrite ^/private/$                      /videos_list.php?is_private=1 last;
    
    rewrite ^/premium/([0-9]+)/$             /videos_list.php?is_private=2&from=$1 last;
    rewrite ^/premium/$                      /videos_list.php?is_private=2 last;
    
    rewrite ^/rss/$                          /rss_videos.php last;
    rewrite ^/rss/tags/([^/]+)/$             /rss_videos.php?tag=$1 last;
    rewrite ^/rss/categories/([^/]+)/$       /rss_videos.php?category=$1 last;
    rewrite ^/rss/models/([^/]+)/$           /rss_videos.php?model=$1 last;
    rewrite ^/rss/sites/([^/]+)/$            /rss_videos.php?cs=$1 last;
    rewrite ^/rss/channels/([^/]+)/$         /rss_videos.php?dvd=$1 last;
    rewrite ^/rss/private/$                  /rss_videos.php?is_private=1 last;
    rewrite ^/rss/premium/$                  /rss_videos.php?is_private=2 last;
    
    rewrite ^/videos/([0-9]+)/([^/]+)/$      /view_video.php?id=$1&dir=$2 last;
    rewrite ^/videos/([^/]+)/$               /view_video.php?dir=$1 last;
    
    # albums ---------------------------------------------------------------------------------------------------------------
    
    rewrite ^/albums/([0-9]+)/$                      /albums_list.php?from=$1 last;
    rewrite ^/albums/$                               /albums_list.php last;
    
    rewrite ^/albums/top-rated/([0-9]+)/$            /albums_list.php?sort_by=rating&from=$1 last;
    rewrite ^/albums/top-rated/$                     /albums_list.php?sort_by=rating last;
    
    rewrite ^/albums/most-popular/([0-9]+)/$         /albums_list.php?sort_by=album_viewed&from=$1 last;
    rewrite ^/albums/most-popular/$                  /albums_list.php?sort_by=album_viewed last;
    
    rewrite ^/albums/categories/([0-9]+)/$           /categories_albums.php?from=$1 last;
    rewrite ^/albums/categories/$                    /categories_albums.php last;
    rewrite ^/albums/categories/([^/]+)/([0-9]+)/$   /albums_list.php?category=$1&from=$2 last;
    rewrite ^/albums/categories/([^/]+)/$            /albums_list.php?category=$1 last;
    
    rewrite ^/albums/sites/([0-9]+)/$                /sponsors_albums.php?from=$1 last;
    rewrite ^/albums/sites/$                         /sponsors_albums.php last;
    rewrite ^/albums/sites/([^/]+)/([0-9]+)/$        /albums_list.php?cs=$1&from=$2 last;
    rewrite ^/albums/sites/([^/]+)/$                 /albums_list.php?cs=$1 last;
    
    rewrite ^/albums/models/([0-9]+)/$               /models_albums.php?from=$1 last;
    rewrite ^/albums/models/$                        /models_albums.php last;
    rewrite ^/albums/models/([^/]+)/([0-9]+)/$       /albums_list.php?model=$1&from=$2 last;
    rewrite ^/albums/models/([^/]+)/$                /albums_list.php?model=$1 last;
    
    rewrite ^/albums/tags/$                          /tags_albums.php last;
    rewrite ^/albums/tags/([^/]+)/([0-9]+)/$         /albums_list.php?tag=$1&from=$2 last;
    rewrite ^/albums/tags/([^/]+)/$                  /albums_list.php?tag=$1 last;
    
    rewrite ^/albums/private/([0-9]+)/$              /albums_list.php?is_private=1&from=$1 last;
    rewrite ^/albums/private/$                       /albums_list.php?is_private=1 last;
    
    rewrite ^/albums/premium/([0-9]+)/$              /albums_list.php?is_private=2&from=$1 last;
    rewrite ^/albums/premium/$                       /albums_list.php?is_private=2 last;
    
    rewrite ^/albums/rss/$                           /rss_albums.php last;
    rewrite ^/albums/rss/tags/([^/]+)/$              /rss_albums.php?tag=$1 last;
    rewrite ^/albums/rss/categories/([^/]+)/$        /rss_albums.php?category=$1 last;
    rewrite ^/albums/rss/models/([^/]+)/$            /rss_albums.php?model=$1 last;
    rewrite ^/albums/rss/sites/([^/]+)/$             /rss_albums.php?cs=$1 last;
    rewrite ^/albums/rss/private/$                   /rss_albums.php?is_private=1 last;
    rewrite ^/albums/rss/premium/$                   /rss_albums.php?is_private=2 last;
    
    rewrite ^/albums/([0-9]+)/([^/]+)/$              /view_album.php?id=$1&dir=$2 last;
    rewrite ^/albums/([^/]+)/$                       /view_album.php?dir=$1 last;
    
    # playlists ------------------------------------------------------------------------------------------------------------
    
    rewrite ^/playlists/([0-9]+)/([^/]+)/$   /view_playlist.php?id=$1&dir=$2 last;
    rewrite ^/playlists/([0-9]+)/$           /playlists_list.php?from=$1 last;
    rewrite ^/playlists/$                    /playlists_list.php last;
    
    # community ------------------------------------------------------------------------------------------------------------
    
    rewrite ^/members/$                              /community.php last;
    
    rewrite ^/members/([0-9]+)/$                     /member_profile_view.php?user_id=$1 last;
    rewrite ^/members/([0-9]+)/videos/$              /member_profile_view.php?user_id=$1&type=videos&ipp=12 last;
    rewrite ^/members/([0-9]+)/albums/$              /member_profile_view.php?user_id=$1&type=albums&ipp=12 last;
    rewrite ^/members/([0-9]+)/friends/$             /member_profile_view.php?user_id=$1&type=friends&ipp=20 last;
    rewrite ^/members/([0-9]+)/favourites/videos/$   /member_profile_view.php?user_id=$1&type=fav_videos&ipp=12 last;
    rewrite ^/members/([0-9]+)/favourites/albums/$   /member_profile_view.php?user_id=$1&type=fav_albums&ipp=12 last;
    rewrite ^/members/([0-9]+)/playlists/$           /member_profile_view.php?user_id=$1&type=playlists&ipp=12 last;
    rewrite ^/members/([0-9]+)/playlists/([0-9]+)/$  /member_profile_view.php?user_id=$1&type=fav_videos&ipp=12&playlist_id=$2 last;
    rewrite ^/members/([0-9]+)/channels/$            /member_profile_view.php?user_id=$1&type=channels&ipp=12 last;
    
    rewrite ^/my/$                                   /member_profile_my.php last;
    rewrite ^/my/videos/$                            /member_profile_my.php?type=videos&ipp=12 last;
    rewrite ^/my/albums/$                            /member_profile_my.php?type=albums&ipp=12 last;
    rewrite ^/my/friends/$                           /member_profile_my.php?type=friends&ipp=20 last;
    rewrite ^/my/favourites/videos/$                 /member_profile_my.php?type=fav_videos&fav_type=0&ipp=12 last;
    rewrite ^/my/favourites/videos-watch-later/$     /member_profile_my.php?type=fav_videos&fav_type=1&ipp=12 last;
    rewrite ^/my/favourites/albums/$                 /member_profile_my.php?type=fav_albums&fav_type=0&ipp=12 last;
    rewrite ^/my/favourites/albums-see-later/$       /member_profile_my.php?type=fav_albums&fav_type=1&ipp=12 last;
    rewrite ^/my/playlists/$                         /member_profile_my.php?type=playlists&ipp=12 last;
    rewrite ^/my/playlists/([0-9]+)/$                /member_profile_my.php?type=fav_videos&ipp=12&playlist_id=$1 last;
    rewrite ^/my/channels/$                          /member_profile_my.php?type=channels&ipp=12 last;
    rewrite ^/my/channels/([0-9]+)/$                 /member_profile_my.php?type=channel&ipp=12&dvd_id=$1 last;
    rewrite ^/my/subscriptions/$                     /member_profile_my.php?type=subscriptions last;
    rewrite ^/my/purchases/$                         /member_profile_my.php?type=purchases last;
    rewrite ^/my/messages/$                          /member_profile_my.php?type=messages last;
    rewrite ^/my/messages/([0-9]+)/$                 /member_profile_my.php?type=conversation&conversation_user_id=$1 last;
    
    rewrite ^/logout/$                               /logout.php last;
    rewrite ^/email/$                                /email_links.php last;
    rewrite ^/payments/$                             /payment_links.php last;
    
    rewrite ^/login/$                                /index.php?mode=async&function=get_block&block_id=logon_logon_form&global=true last;
    rewrite ^/login-required/$                       /index.php?mode=async&function=get_block&block_id=logon_logon_form&global=true&error=only_for_members last;
    rewrite ^/signup/$                               /index.php?mode=async&function=get_block&block_id=signup_signup_form_simple&global=true last;
    rewrite ^/upgrade/$                              /index.php?mode=async&function=get_block&block_id=upgrade_upgrade_access&global=true last;
    rewrite ^/reset-password/$                       /index.php?mode=async&function=get_block&block_id=signup_reset_password_form&global=true last;
    rewrite ^/resend-confirmation/$                  /index.php?mode=async&function=get_block&block_id=signup_resend_confirmation_form&global=true last;
    
    rewrite ^/create-playlist/$                      /index.php?mode=async&function=get_block&block_id=playlist_edit_edit_playlist_form&global=true last;
    rewrite ^/select-playlist/$                      /index.php?mode=async&function=get_block&block_id=list_playlists_playlists_selector&global=true last;
    rewrite ^/edit-playlist/([0-9]+)/$               /index.php?mode=async&function=get_block&block_id=playlist_edit_edit_playlist_form&global=true&playlist_id=$1 last;
    
    rewrite ^/create-channel/$                       /index.php?mode=async&function=get_block&block_id=dvd_edit_edit_channel_form&global=true last;
    rewrite ^/edit-channel/([0-9]+)/$                /index.php?mode=async&function=get_block&block_id=dvd_edit_edit_channel_form&global=true&dvd_id=$1 last;
    
    rewrite ^/edit-profile/$                         /index.php?mode=async&function=get_block&block_id=member_profile_edit_edit_profile_forms_advanced&global=true&action=change_profile last;
    rewrite ^/change-password/$                      /index.php?mode=async&function=get_block&block_id=member_profile_edit_edit_profile_forms_advanced&global=true&action=change_pass last;
    rewrite ^/change-email/$                         /index.php?mode=async&function=get_block&block_id=member_profile_edit_edit_profile_forms_advanced&global=true&action=change_email last;
    rewrite ^/delete-profile/$                       /index.php?mode=async&function=get_block&block_id=member_profile_delete_delete_profile_form&global=true last;
    
    rewrite ^/invite/$                               /index.php?mode=async&function=get_block&block_id=invite_friend_invite_friend_form&global=true last;
    rewrite ^/feedback/$                             /index.php?mode=async&function=get_block&block_id=feedback_feedback_form&global=true last;
    rewrite ^/captcha/([^/]*)/$                      /index.php?mode=async&function=show_security_code&captcha_id=$1 last;
    
    rewrite ^/upload-video/$                         /member_profile_my.php?type=upload_video last;
    rewrite ^/upload-channel/([0-9]+)/$              /member_profile_my.php?type=upload_video&dvd_id=$1 last;
    rewrite ^/upload-video-preview/([^/]*)/$         /member_profile_my.php?mode=async&action=video_preview&file=$1 last;
    rewrite ^/edit-video/([0-9]+)/$                  /member_profile_my.php?type=upload_video&video_id=$1 last;
    
    rewrite ^/upload-photos/$                        /member_profile_my.php?type=upload_album last;
    rewrite ^/upload-photos-preview/([^/]*)/$        /member_profile_my.php?mode=async&action=album_preview&files=$1 last;
    rewrite ^/edit-photos/([0-9]+)/$                 /member_profile_my.php?type=upload_album&album_id=$1 last;
    
    rewrite ^/categories-selector/$                  /index.php?mode=async&function=get_block&block_id=list_categories_categories_selector&global=true last;
    rewrite ^/models-selector/$                      /index.php?mode=async&function=get_block&block_id=list_models_models_selector&global=true last;
    
    rewrite ^/upload-progress/([^/]*)/$              /admin/include/get_upload_status_url.php?file=$1 last;
    
    # SYSTEM / DO NOT CHANGE -----------------------------------------------------------------------------------------------
    rewrite ^/sitemap/$ /sitemap.php last;
    rewrite ^/sitemap\.xml$ /sitemap.php last;
    rewrite ^/embed/(.+)$ /player/iframe_embed.php?video_id=$1 last;
    rewrite ^/related_videos_html/([0-9]+)/?$ /related_videos_html.php?video_id=$1 last;
    rewrite ^/get_file/([0-9]+)/([^/]*)/(.*)$ /get_file.php?sg_id=$1&hash=$2&file=$3 last;
    rewrite ^/get_image/([0-9]+)/([^/]*)/(.*)/$ /get_image.php?sg_id=$1&hash=$2&file=$3 last;
    # END SYSTEM -----------------------------------------------------------------------------------------------------------
    Attached Files
Working...
X