LNMP的搭建及URL重寫測試

內容:php

一、LNMP的搭建html

二、搭建基於LNMP的discuz論壇(www.hill.com)mysql

三、實現httpsnginx

四、實現訪問http時自動跳轉至https以及防盜鏈設置、URL重寫測試sql


1、LNMP的搭建apache

        咱們知道,在apache與php的結合方式有三種,而nginx與php的結合目前只有一種是行之有效的:php-fpmcentos

        一、yum直接安裝快速搭建LNMP,官方下載nginx的預安裝包(rpm包),固然也能夠編譯安裝瀏覽器

#yum install -y prce-devel zlib-devel openssl-devel php-fpm php-mysql mariadb-server
  # yum install nginx-1.10.0-1.el7.ngx.x86_64.rpm

        

        二、添加ngixn虛擬主機bash

[root@localhost pma]# cat /etc/nginx/conf.d/hill.conf 
server {
listen 80;
server_name www.hill.com;
root /var/www/html/hill;
location  /  {
index index.php index.html;
}
location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/hill$fastcgi_script_name;
include fastcgi_params;
}
}

      

          三、啓動相關服務,添加host文件測試服務器

[root@localhost hill]# curl -I www.hill.com
HTTP/1.1 200 OK
Server: nginx/1.10.0
Date: Mon, 24 Oct 2016 22:17:52 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16

wKiom1gOy1CiWR7YAACXYLVdjJ4762.png

        四、測試phpMyAdmin成功


wKioL1gOy2DT1ar-AAFMbUerlNc666.png



        5安裝論壇測試訪問發帖成功:

wKioL1gOy3CCDMVwAAD02Uh1Ltw442.png




2、nginx的https實現

        一、搭建根CA服務器(這裏在同一臺機子上)

[root@localhost hill]# (umask 066 ; openssl genrsa -out /etc/pki/CA/)
certs/    crl/      newcerts/ private/  
[root@localhost hill]# (umask 066 ; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
...+++
..............................................................................+++
e is 65537 (0x10001)
[root@localhost hill]# openssl req -new -x509 -days 365 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/
certs/    crl/      newcerts/ private/  
[root@localhost hill]# openssl req -new -x509 -days 365 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BEIJING
Locality Name (eg, city) [Default City]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:hill.com  
Organizational Unit Name (eg, section) []:ca
Common Name (eg, your name or your server's hostname) []:ca.hill.com
Email Address []:
[root@localhost hill]#


        二、nginx申請證書籤署

[root@localhost hill]# mkdir -p /etc/nginx/ssl
[root@localhost hill]# (umask 066 ; openssl genrsa -out /etc/nginx/ssl/nginx.key 2048)
Generating RSA private key, 2048 bit long modulus
....+++
......................+++
e is 65537 (0x10001)
[root@localhost hill]# openssl req -new -days 365 -key /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BEIJIN
Locality Name (eg, city) [Default City]:^C
[root@localhost hill]# openssl req -new -days 365 -key /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BEIJING
Locality Name (eg, city) [Default City]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:hill.com
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www.hill.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:


    三、根CA簽署證書

[root@localhost CA]# openssl ca -in /etc/nginx/ssl/nginx.csr -out /etc/nginx/ssl/nginx.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Oct 24 22:55:33 2016 GMT
            Not After : Oct 24 22:55:33 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BEIJING
            organizationName          = hill.com
            organizationalUnitName    = ops
            commonName                = www.hill.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                06:98:EC:97:95:A6:7A:29:D4:DE:F4:C7:98:ED:D1:01:F9:16:07:8A
            X509v3 Authority Key Identifier: 
                keyid:F4:2C:6B:72:C4:D0:B5:CF:6F:B8:4E:A5:E1:A6:73:27:6D:6E:88:D3
Certificate is to be certified until Oct 24 22:55:33 2017 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost CA]# 
[root@localhost CA]# 
[root@localhost CA]# ll /etc/nginx/ssl/
total 16
-rw-r--r--. 1 root root 4457 Oct 25 06:55 nginx.crt
-rw-r--r--. 1 root root 1005 Oct 25 06:54 nginx.csr
-rw-------. 1 root root 1679 Oct 25 06:53 nginx.key


    四、編輯nginx虛擬主機的配置文件

[root@localhost CA]# cat /etc/nginx/conf.d/hill.conf 
server {
listen 443 ssl;
server_name www.hill.com;
root /var/www/html/hill;
location  /  {
index index.php index.html;
}
location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/hill$fastcgi_script_name;
include fastcgi_params;
}
ssl on;
  ssl_certificate /etc/nginx/ssl/nginx.crt;  
    ssl_certificate_key /etc/nginx/ssl/nginx.key; 
    ssl_session_cache shared:sslcache:20m;
}
[root@localhost CA]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost CA]# nginx -s reload
[root@localhost CA]# ss -tanl
State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
LISTEN      0      128                      127.0.0.1:9000                                         *:*                  
LISTEN      0      128                              *:47112                                        *:*                  
LISTEN      0      50                               *:3306                                         *:*                  
LISTEN      0      128                              *:111                                          *:*                  
LISTEN      0      128                              *:22                                           *:*                  
LISTEN      0      100                      127.0.0.1:25                                           *:*                  
LISTEN      0      128                              *:443                                          *:*                  
LISTEN      0      128                             :::111                                         :::*                  
LISTEN      0      128                             :::22                                          :::*                  
LISTEN      0      100                            ::1:25                                          :::*                  
LISTEN      0      128                             :::56155                                       :::*


    五、瀏覽器打開測試成功 

    

wKioL1gOy6vSVCxaAADfR2OPITw217.png


更多文章請關注 個人博客

4、實現訪問http時自動跳轉至https以及防盜鏈設置、URL重寫測試


        一、http自動跳轉https測試:

[root@MiWiFi-R3-srv hill]# cat /etc/nginx/conf.d/hill.conf 
server {
listen 443 ssl ;
server_name www.hill.com;
location  /  {
index index.php index.html;
root /var/www/html/hill;
valid_referers none block server_names ~hill\.com;
if ($invalid_referer)
{
return 111;
}
}
location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/hill$fastcgi_script_name;
include fastcgi_params;
}
ssl on;
  ssl_certificate /etc/nginx/ssl/nginx.crt;  
    ssl_certificate_key /etc/nginx/ssl/nginx.key; 
    ssl_session_cache shared:sslcache:20m;
}
server{
listen 80;
server_name www.hill.com;
rewrite ^(.*)$  https://$host$1 permanent ;
}

   


        二、防盜鏈測試:

        添加配置:


valid_referers none block server_names ~hill\.com;
if ($invalid_referer)
{
return 111;
}

     

        測試正常

[root@MiWiFi-R3-srv hill]# cat /etc/nginx/conf.d/hill.conf 
server {
listen 443 ssl ;
server_name www.hill.com;
location  /  {
index index.php index.html;
root /var/www/html/hill;
if ($http_user_agent ~* Edge )
{
rewrite ^(.*)$ /test1.html break;
}
if ($http_user_agent ~* Mathon )
{
rewrite ^(.*)$ /test2.html break;
}
if ($http_user_agent ~* firefox )
{
rewrite ^(.*)$ /test3.html break;
}
valid_referers none block server_names ~hill\.com;
if ($invalid_referer)
{
return 111;
}
}
location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/hill$fastcgi_script_name;
include fastcgi_params;
}
ssl on;
  ssl_certificate /etc/nginx/ssl/nginx.crt;  
    ssl_certificate_key /etc/nginx/ssl/nginx.key; 
    ssl_session_cache shared:sslcache:20m;
}
server{
listen 80;
server_name www.hill.com;
rewrite ^(.*)$  https://$host$1 permanent ;
}

[12:36 root@centos6.8~]# curl -I -k -e "www.baidu.com" https://www.hill.com
HTTP/1.1 111 
Server: nginx/1.10.0
Date: Tue, 25 Oct 2016 10:50:40 GMT
Content-Type: application/octet-stream
Content-Length: 0
Connection: keep-alive


        三、URL重寫測試:不一樣的代理實現不一樣的效果

        添加配置:


if ($http_user_agent ~* Edge )
{
rewrite ^(.*)$ /test1.html break;
}
if ($http_user_agent ~* Mathon )
{
rewrite ^(.*)$ /test2.html break;
}
if ($http_user_agent ~* firefox )
{
rewrite ^(.*)$ /test3.html break;
}


         測試成功:


[root@MiWiFi-R3-srv hill]# cat /etc/nginx/conf.d/hill.conf 
server {
listen 443 ssl ;
server_name www.hill.com;
location  /  {
index index.php index.html;
root /var/www/html/hill;
if ($http_user_agent ~* Edge )
{
rewrite ^(.*)$ /test1.html break;
}
if ($http_user_agent ~* Mathon )
{
rewrite ^(.*)$ /test2.html break;
}
if ($http_user_agent ~* firefox )
{
rewrite ^(.*)$ /test3.html break;
}
valid_referers none block server_names ~hill\.com;
if ($invalid_referer)
{
return 111;
}
}
location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/hill$fastcgi_script_name;
include fastcgi_params;
}
ssl on;
  ssl_certificate /etc/nginx/ssl/nginx.crt;  
    ssl_certificate_key /etc/nginx/ssl/nginx.key; 
    ssl_session_cache shared:sslcache:20m;
}
server{
listen 80;
server_name www.hill.com;
rewrite ^(.*)$  https://$host$1 permanent ;
}

        

        測試效果有效

wKiom1gOzEGhHhemAAAbclxJCDk416.png

wKioL1gOzEHgQB5HAAAyh_uIt6s003.png




        附:http核心模塊的內置變量

$uri: 當前請求的uri,不帶參數;
$request_uri: 請求的uri,帶完整參數;
$host: http請求報文中host首部;若是請求中沒有host首部,則以處理此請求的虛擬主機的主機名代替;
$hostname: nginx服務運行在的主機的主機名;
$remote_addr: 客戶端IP
$remote_port: 客戶端Port
$remote_user: 使用用戶認證時客戶端用戶輸入的用戶名;
$request_filename: 用戶請求中的URI通過本地root或alias轉換後映射的本地的文件路徑;
$request_method: 請求方法
$server_addr: 服務器地址
$server_name: 服務器名稱
$server_port: 服務器端口
$server_protocol: 服務器向客戶端發送響應時的協議,如http/1.1, http/1.0
$scheme: 在請求中使用scheme, 如https://www.domain.com/中的https;
$http_HEADER: 匹配請求報文中指定的HEADER,$http_host匹配請求報文中的host首部
$sent_http_HEADER: 匹配響應報文中指定的HEADER,例如$http_content_type匹配響應報文中的content-type首部;
$document_root:當前請求映射到的root配置;

更多文章請關注 個人博客

相關文章
相關標籤/搜索