nginx實戰

(一)nginx反向代理

1. 安裝nginx用到的源:php

[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/

2. 環境部署:html

centos7.5node

反向代理服務器IP:192.168.42.173mysql

web1服務器IP:192.168.42.122nginx

web2服務器IP:192.168.42.172web

3. 下載nginxsql

yum install -y nginx

4.配置nginx文件,咱們實現這樣一個效果,靜態文件都被代理到192.168.42.122,動態文件都被調度到192.168.42.172,實現動靜分離數據庫

[root@jam ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;
   index index.html index.php;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location ~\.html$ { proxy_pass http://192.168.42.122; } location ~^/.*(\.php)$ { proxy_pass http://192.168.42.172; }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

5.進行語法檢測,並進行重啓nginxvim

[root@jam ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@jam ~]# systemctl restart nginx

6.配置web服務端centos

web服務器192.168.42.122部署:
[root@node2 ~]# yum install -y httpd [root@node2 ~]# echo "i am jamhsiao's fans" > /var/www/html/index.html [root@node2 ~]# cat /var/www/html/index.html i am jamhsiao's fan
[root@node2 ~]# systemctl restart httpd

web服務器192.168.42.172部署:
[root@node3 ~]# yum install php -y
[root@node3 ~]# vim /var/www/html/index.php [root@node3 ~]# cat /var/www/html/index.php <?php phpinfo(); ?>
[root@node3 ~]# systemctl restart httpd

7.web服務器關閉安全服務

[root@node2 ~]# iptables -F

[root@node3 ~]# iptables -F

8.訪問靜態頁面

9.訪問動態界面

 

(二)nginx實現負載均衡

1.環境部署:

centos7.5

nginx服務器IP:192.168.42.173

web1服務器端IP:192.168.42.122

web2服務器端IP:192.168.42.172

2. 編輯配置文件

[root@jam ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    upstream jam {
    server 192.168.42.122 weight=1 max_fails=3 fail_timeout=5;
    server 192.168.42.172 weight=2 max_fails=3 fail_timeout=5;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;
    index index.html index.php;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
#  location ~ \.html$ {
#        proxy_pass http://192.168.42.122;
#        }
#        location ~^/.*(\.php)$ {
#        proxy_pass http://192.168.42.172;
#    }

    location / {
    proxy_pass http://jam/;
    proxy_set_header host $proxy_host;
    proxy_set_header realip $remote_addr;
    }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

3. 檢測語法問題並重啓nginx

[root@jam ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@jam ~]# systemctl restart nginx

4. web端與反向代理同樣

5.瀏覽器訪問(輸入服務端IP)

 

 若是沒有出現輪詢,多是瀏覽器的問題,建議換個瀏覽器試試。

(三)LNMP架構上線動態網站

1. 下載php-fpm須要用到下面的源

[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/

2. 安裝服務程序

[root@jam yum.repos.d]# yum install nginx php php-mysql php-fpm mariadb-server -y

3.修改配置文件

[root@jam ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
#    upstream jam {
#    server 192.168.42.122 weight=1 max_fails=3 fail_timeout=5;
#    server 192.168.42.172 weight=2 max_fails=3 fail_timeout=5;
#    }
    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
 listen 80;
        server_name  _;
        root         /var/www/html;
    index index.html index.php;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
#  location ~ \.html$ {
#        proxy_pass http://192.168.42.122;
#        }
 location ~^/.*(\.php)$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; }

#    location / {
#    proxy_pass http://jam/;
#    proxy_set_header host $proxy_host;
#    proxy_set_header realip $remote_addr;
#    }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

4. 重啓nginx和php-fpm

[root@jam ~]# systemctl restart nginx
[root@jam ~]# systemctl restart php-fpm

5. 上傳wordpress並修改配置文件

如下操做要在 /var/www/html 目錄下進行,我懶,就不改了。
[root@jam ~]# rz [root@jam ~]# unzip wordpress-3.3.1-zh_CN.zip [root@jam ~]# ls jam wordpress wordpress-3.3.1-zh_CN.zip [root@jam ~]# mv wordpress/* . [root@jam ~]# ls index.php wp-cron.php jam wp-includes license.txt wp-links-opml.php readme.html wp-load.php wordpress wp-login.php wordpress-3.3.1-zh_CN.zip wp-mail.php wp-activate.php wp-pass.php wp-admin wp-register.php wp-app.php wp-settings.php wp-blog-header.php wp-signup.php wp-comments-post.php wp-trackback.php wp-config-sample.php xmlrpc.php wp-content [root@jam ~]# cp wp-config-sample.php wp-config.php [root@jam ~]# vim wp-config.php /** WordPress 數據庫的名稱 */ define('DB_NAME', 'jam'); /** MySQL 數據庫用戶名 */ define('DB_USER', 'jam'); /** MySQL 數據庫密碼 */ define('DB_PASSWORD', '0330');

6.重啓數據庫,建立用戶並給權限

[root@jam ~]# systemctl restart mariadb
[root@jam ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database jam;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on *.* to jam@'localhost' ident ified by "0330";
Query OK, 0 rows affected (0.00 sec)

7.瀏覽器訪問

以上部署完成,下一篇我會在此基礎上作一個小項目:Linux 系統中部署 LNMP 高可用負載均衡架構集羣實現動態博客。

敬請期待!!!

相關文章
相關標籤/搜索