源碼編譯Nginx服務配置

一、實驗環境:

RHEL7.0 172.25.254.1 server1.example.com  firewalld disable

二、實驗內容:

    1.源碼安裝Nginx

        nginx-1.9.14.tar.gz        下載源碼包

        tar zxf nginx-1.9.14.tar.gz

        cd nginx-1.9.14/

        vim auto/cc/gcc

        # debug
        #CFLAGS="$CFLAGS -g"    #關閉debug(由於使用gcc編譯器,所以關閉gcc編譯時安裝的debug功能)

        vim src/core/nginx.h

        #define NGINX_VER          "nginx/"        #隱藏nginx版本號

        useradd -u 800 -M -d /usr/local/nginx -s /sbin/nologin nginx

        yum install -y gcc prce-devel openssl-devel

        ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module

        make             編譯

        make install        安裝

        ls /usr/local/nginx        編譯安裝完成後查看

        conf  html  logs  sbin

    2.配置:

        [root@server1 nginx-1.9.14]# cd /usr/local/nginx/

        [root@server1 nginx]# vim conf/nginx.conf
        worker_processes  1;        #通過lscpu來查看有幾個cpu

        events {
            use epoll;        #採用異步非阻塞模式 apache --select 同步阻塞機制 io複用模型類型                                      worker_connections  1024;
        }

        [root@server1 nginx]# vim /etc/profile        ##添加nginx執行路徑

        export PATH=$PATH:/usr/local/nginx/sbin

        [root@server1 nginx]# source /etc/profile
        [root@server1 nginx]# nginx -t
            ##檢查nginx配置是否有誤
        nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
        [root@server1 nginx]# nginx         ##啓動nginx服務

        nginx -s reload        #重啓

        nginx -s stop             #關閉

    測試:

        [root@server1 nginx]# curl -I localhost
        HTTP/1.1 200 OK
        Server: nginx/
        Date: Wed, 14 Sep 2016 16:12:37 GMT
        Content-Type: text/html
        Content-Length: 612
        Last-Modified: Wed, 14 Sep 2016 15:37:42 GMT
        Connection: keep-alive
        ETag: "57d96ec6-264"
        Accept-Ranges: bytes

        [root@server1 nginx]# cd html/        默認發佈目錄
        [root@server1 html]# ls

        50x.html  index.html

    網頁測試:

    wKioL1fZB4PxYfYwAACzICrr6G0433.png


    3.添加HTTPS:

        [root@server1 nginx]# vim conf/nginx.conf

        # HTTPS server        #開啓HTTPS功能

        server {
            listen       443 ssl;
            server_name  localhost;

            ssl_certificate      cert.pem;
            ssl_certificate_key  cert.pem;        #修改證書名

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

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

            location / {
                root   html;
               index  index.html index.htm;
            }
        }

    }
        [root@server1 nginx]# cd /etc/pki/tls/certs/
        [root@server1 certs]# make cert.pem        
#新建證書

        Country Name (2 letter code) [XX]:CN
        State or Province Name (full name) []:shannxi
        Locality Name (eg, city) [Default City]:xi'an
        Organization Name (eg, company) [Default Company Ltd]:redhat
        Organizational Unit Name (eg, section) []:Linux
        Common Name (eg, your name or your server's hostname) []:localhost
        Email Address []:[email protected]
        [root@server1 certs]# cp cert.pem /usr/local/nginx/conf/
        [root@server1 certs]# nginx -t
        #檢查配置是否有誤
        nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
        [root@server1 certs]# nginx -s reload    #重啓服務

    測試:

    wKioL1fZClngKNy_AAEC17w3z7U839.png

wKiom1fZCmry2l8uAAEWKGkQ8i8630.png

wKioL1fZCoSiibk4AAB9DkdRRf0266.png

    4.虛擬主機:

        [root@server1 nginx]# vim conf/nginx.conf

        在http {} 中添加

        server {
                listen 80;
                server_name www.xiaoze.com;
                location / {
                        root /virtual/xiaoze/html;
                        index index.html;
                }
        }
        server {
                listen 80;
                server_name www.westos.com;
                location / {
                        root /virtual/westos/html;
                        index index.html;
                }
        }

        [root@server1 nginx]# mkdir -p /virtual/xiaoze/html
        [root@server1 nginx]# mkdir -p /virtual/westos/html
        [root@server1 nginx]# echo www.xiaoze.com > /virtual/xiaoze/html/index.html
        [root@server1 nginx]# echo www.westos.com > /virtual/westos/html/index.html
        [root@server1 nginx]# vim /etc/hosts

        172.25.254.1 www.xiaoze.com
        172.25.254.1 www.westos.com

        [root@server1 nginx]# nginx -t
        nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
        [root@server1 nginx]# nginx -s reload

    測試:

wKiom1fZDxOAsp2-AAAqhJGerE8846.png

wKiom1fZDyGxizEeAAApr8F8ltg050.png

    5.Nginx監控小插件(網站信息統計)

        [root@server1 nginx]# vim conf/nginx.conf

        server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /message {            #添加的內容
                stub_status on;
                access_log off;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root html;
        }
        [root@server1 nginx]# nginx -t
        
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
        [root@server1 nginx]# nginx -s reload

    測試:

wKioL1fZEa_QdqciAABU7Qo021Y951.png

    6.網頁重寫(自動轉到HTTPS)

    [root@server1 nginx]# vim conf/nginx.conf

    server {
                listen 80;
                server_name login.xiaoze.com;
                rewrite ^(.*)$ https://$host$1 permanent;
                location / {
                        root /virtual/login/html;
                        index index.html;
                }
        }

        [root@server1 nginx]# mkdir -p /virtual/login/html
        [root@server1 nginx]# echo login.xiaoze.com > /virtual/login/html/index.html
        [root@server1 nginx]# vim /etc/hosts

        172.25.254.1 login.xiaoze.com

        [root@server1 nginx]# nginx -t
        nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
        [root@server1 nginx]# nginx -s reload
    測試:

wKiom1fZFVbzm0twAADL4Yd4UxM998.png

wKiom1fZFWSRdEPIAAC6H7UAIpQ433.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章