How to Install Nginx, Enable HTTPS, and Use URL Rewrite for Mobile‑Specific Pages

This guide walks through setting up a virtual machine, installing and compiling Nginx, configuring its main, event, and http blocks, creating SSL certificates, and applying URL rewrite rules to serve different pages based on the client’s browser or device.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Install Nginx, Enable HTTPS, and Use URL Rewrite for Mobile‑Specific Pages

Preface

This article demonstrates using Nginx as a web server and applying URL rewrite to redirect mobile requests to a dedicated mobile page.

Environment Setup

The author uses a single virtual machine bridged to a router for mobile access, with IP 192.168.1.103.

Nginx Overview

Nginx (pronounced "engine x") was created by Igor Sysoev and is the third‑most popular web server worldwide. It is lightweight, supports reverse proxy, URL rewrite, low memory usage, high concurrency (up to 50 000 connections), hot deployment, and a high‑performance I/O model. Taobao built Tengine on top of Nginx.

Compile and Install Nginx

Install required packages:

[root@server1 ~]# yum groupinstall "Development Tools" "Server Platform Development"
[root@server1 ~]# yum install pcre-devel openssl-devel zlib-devel -y
[root@server1 ~]# tar xf nginx-1.6.1.tar.gz -C /usr/src/
[root@server1 ~]# cd /usr/src/
[root@server1 src]# cd nginx-1.6.1/
[root@server1 nginx-1.6.1]# groupadd -r nginx
[root@server1 nginx-1.6.1]# useradd -r -g nginx nginx
[root@server1 nginx-1.6.1]# ./configure --prefix=/usr/src/nginx --sbin-path=/sbin/ --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --user=nginx --group=nginx --with-http_gzip_static_module
[root@server1 nginx-1.6.1]# make && make install

Config File Explanation

The main configuration file is /etc/nginx/nginx.conf and is divided into three sections: main, events, and http.

main and events

user User_Name [Group_name];
error_log /path/to/error_log;
error_log /path/to/error_log notice;
pid /path/to/pidfile;

worker_processes number;
worker_cpu_affinity cpumask ...;
time_resolution interval;
worker_priority number;

accept_mutex on|off;
use [epoll|rtsig|select|poll];
worker_connections number;

http block basics

1. server{}: defines a virtual host
   example:
   server {
       listen 80;
       server_name www.anyisalin.com;
       root "/htdocs/www";
   }
2. listen: listen address[:port];
3. server_name: supports exact, wildcard, and regex matching.
4. root: root path;
5. location: location [=] [~] [~*] [^~] URL {...}
   = exact match, ~ regex (case‑sensitive), ~* regex (case‑insensitive), ^~ prefix match.
   example locations for static files and PHP.

Configure Nginx

Basic Web Server

Edit nginx.conf as follows:

server {
    listen 80;
    server_name www.anyisalin.com;
    location / {
        root /htdocs/html;
        index index.html index.htm;
        error_page 404 =200 404.html;
    }
}

Create web files:

[root@server1 /]# mkdir -p htdocs/html
[root@server1 /]# cd htdocs/html/
[root@server1 html]# echo "<h1>www.anyisalin.com</h1>" >> index.html
[root@server1 html]# echo "Sorry, Page Not Found" > 404.html
[root@server1 html]# nginx -t
[root@server1 html]# nginx

Test page access (screenshots omitted).

Enable HTTPS

Create a private CA and sign an Nginx certificate (OpenSSL commands shown).

# Create CA key and certificate
[root@server1 CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
[...]
# Create Nginx key and CSR
[root@server1 ssl]# (umask 077; openssl genrsa -out nginx.key 1024)
[...]
# Sign certificate
[root@server1 ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365

Update nginx.conf for SSL:

server {
    listen 443 ssl;
    server_name www.anyisalin.com;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    location / {
        root /htdocs/html;
        index index.html index.htm;
        error_page 404 =200 404.html;
    }
}

Reload Nginx and verify HTTPS (screenshots omitted).

URL Rewrite for Different Browsers

Rewrite Syntax

rewrite regex replacement flag;

Flags: last, break, redirect (302), permanent (301).

User‑Agent Based Rewrite

location / {
    root /htdocs/html;
    index index.html index.htm;
    error_page 404 =200 404.html;

    if ($http_user_agent ~* Android) { rewrite ^(.*)$ /Moblie/$1 break; }
    if ($http_user_agent ~* Chrome)  { rewrite ^(.*)$ /Chrome/$1 break; }
    if ($http_user_agent ~* MSIE)    { rewrite ^(.*)$ /IE/$1 break; }
}

Create directories and simple index pages for each user‑agent:

[root@server1 /]# mkdir -p /htdocs/html/{Chrome,IE,Moblie}
[root@server1 /]# echo "Welcome Moblie" > /htdocs/html/Moblie/index.html
[root@server1 /]# echo "Welcome Chrome" > /htdocs/html/Chrome/index.html
[root@server1 /]# echo "Welcome IE" > /htdocs/html/IE/index.html

Test with mobile, Chrome, and IE browsers (screenshots omitted).

Conclusion

The article introduced basic Nginx usage as a web server, SSL configuration, and user‑agent based URL rewriting. Future posts will cover using Nginx as a reverse proxy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxNginxWeb serverURL RewriteHTTPS
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.