Nginx安裝以及配置詳解

Nginx簡介

Nginx是什麼?

Nginx是一款輕量級web服務器,也是一款反向代理服務器

Nginx能幹什麼

Nginx能幹的事情很多,這裏簡要羅列一下

  • 可直接支持rail和PHP程序
  • 可作爲HTTP反向代理服務器
  • 作爲負載均衡服務器
  • 作爲郵件代理服務器
  • 幫助實現前端動靜分離

Nginx特點

  • 高穩定
  • 高性能
  • 資源佔用少
  • 功能豐富。以及豐富的插件
  • 模塊化的結構
  • 支持熱部署

Nginx安裝(Windows)

  • 官方下載地址-下載Windows穩定版即可

  • 下載成功之後解壓到指定目錄

  • 點擊nginx.exe,運行nginx服務,此時打開瀏覽器,訪問本地域名就可以訪問到nginx服務了

    • 本地域名是localhost,本地IP是127.0.0.1 ,
    • 如果修改過 C:\Windows\System32\drivers\etc\host 空白處添加 127.0.0.1 misty.com 可以通過本地域名訪問
  • 記事本打開打開安裝目錄下的${nginx}/conf/nginx.conf在節點之間加入 include vhost/*.conf;引入相關配置

  • 在vhost目錄(nginx.conf同級) 添加配置文件,例如

server {
    listen 80;
    autoindex on;
    server_name misty.com www.misty.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:8080;
        add_header Access-Control-Allow-Origin *;
    }
}

在nginx安裝目錄的根目錄執行 nginx.exe -s reload,重新加載nginx配置文件

* `D:\Program Files\nginx-1.16.0>nginx.exe  -s reload`

Nginx安裝(Linux)

  • 安裝gcc yum install gcc
    • 備註:可以輸入 gcc -v 查詢版本信息,看系統是否已經安裝
  • 安裝pcre yum install pcre pcre-devel
  • 安裝zlib yum install zlib zlib-devel
  • 安裝openssl yum install openssl openssl-devel
    • 備註:如需支持ssl,才需安裝openssl

綜合命令 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

  • 下載源碼包 ,選擇穩定版本,解壓縮安裝 官網地址

    • wget http://nginx.org/download/nginx-1.12.2.tar.gz
  • 解壓 tar -zxvf nginx-1.12.2.tar.gz

  • 安裝

    • 進入nginx目錄之後執行./configure,可能需要sudo權限,
      • 也可以指定安裝目錄,增加參數 --prefix=/usr/nginx
      • 如果不指定路徑,可以通過 whereis nginx 進行查詢
      • 默認安裝在 /usr/local/nginx
    • 繼續執行 make
    • 繼續執行 make install
  • 啓動 沒有指定安裝路徑的情況下 默認/usr/local/nginx

    • 執行 .${nginx}/sbin/nginx
    • 默認nginx監聽80端口,此時打開瀏覽器輸入域名就可以進入nginx默認頁面了

nginx常用命令

  • 測試配置文件

    • 安裝路徑下的/nginx/sbin/nginx -t
  • 啓動命令

    • 安裝路徑下的 /nginx/sbin/nginx
  • 停止命令

    • 安裝路徑下的 /nginx/sbin/nginx -s stop 或者 .${nginx}/sbin/nginx -s quit
  • 重啓命令

    • 安裝路徑下的 /nginx/sbin/nginx -s reload
  • 查看進程命令

    • ps -ef |grep nginx
  • 平滑重啓

    • kill -HUB [nginx主進程號,也就是查看進程命令查到的pid]

增加防火牆訪問權限

  • CentOS 6
    1. sudo vim /etc/sysconfig/iptables
    2. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 
    3. 保存退出
    4. 重啓防火牆 sudo service iptables restart 
   sudo firewall-cmd --zone=public --add-port=3000/tcp --permanent
   sudo firewall-cmd --reload

nginx 開啓網站https訪問

參考本站這一篇文章wordpress開啓https訪問

虛擬域名的配置以及測試驗證

  • 配置步驟

    • 編輯 sudo vim /usr/local/nginx/conf/nginx.conf
      • 在server節點下增加 include vhost/*.conf, 將和 nginx.conf 同級目錄下的vhost下的配置文件導入
      • 保存退出
        img
  • /usr/local/nginx/conf/ 目錄新建vhost文件夾 即 /usr/local/nginx/conf/vhost

  • 在vhost目錄創建域名轉發配置文件,詳情參考下文nginx詳情配置

  • 啓動(重啓)驗證

    • 啓動 ${nginx}/sbin/nginx
    • 重啓 ${nginx}/sbin/nginx -s reload
 ${nginx}  代表安裝在系統中的路徑,例如 /usr/local/nginx
  • 訪問驗證
    • 使用默認80端口訪問驗證 http://localhost:80http://127.0.0.1:80

nginx配置實例

指向端口

server {
listen 80;
server_name api.imisty.cn;
client_max_body_size 200M;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#root /devsoft/apache-tomcat-7.0.73/webapps/blog;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
        }
location / {
        proxy_pass http://127.0.0.1:8080/;
        add_header Access-Control-Allow-Origin '*';
        }
}

指向目錄:本段配置指向download目錄 ,autoindex on代表自動創建索引

  server {
    listen 80;
    autoindex on;
    server_name download.imisty.cn;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    location / {
        root /download;
        add_header Access-Control-Allow-Origin *;
    }
}

img

既指向端口又指向目錄

server {
listen 80;
autoindex on;
server_name imisty.cn www.imisty.cn;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp index.php;
if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
        }

location = / {
        root /product/blog/dist/view;
        index index.html;
}

location ~ .*\.html$ {
        root /product/blog/dist/view;
        index index.html;
}

location / {
        proxy_pass http://127.0.0.1:8080/;
        }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        proxy_pass http://127.0.0.1:8080;
        expires 30d;
        }

location ~ .*\.(js|css)?$ {
        proxy_pass http://127.0.0.1:8080;
        expires 7d;
        }
}

注意 nginx的域名配置指向目錄的時候,autoindex on|off 屬性,可以設置是否創建索引,設置成off,首頁403,但是內容依舊可以訪問,只是沒有暴露索引

補充一段線上的wordpress博客配置,這裏省去了https部分,屬於比較標準的即指向端口有指向目錄的配置

server {
        listen          80;
        server_name     www.imisty.cn imisty.cn;
        #非常關鍵不然 http也可以訪問
        return 301 https://www.imisty.cn$request_uri;
        root            /var/www/html/wordpress;
        index           index.html index.htm index.php;
        client_max_body_size 100M;
        proxy_intercept_errors on;

        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
        }

        location ~ .*\.php(\/.*)*$ {
            include fastcgi.conf;
            fastcgi_index  index.php;
            fastcgi_pass  127.0.0.1:9000;

            fastcgi_connect_timeout 300;

            fastcgi_send_timeout 300;

            fastcgi_read_timeout 300;

        }

		error_log  logs/error_wordpress.log;
        access_log logs/misty.log combined;
    }

  • 配置完成或者修改配置之後一定要記得 ./nginx -s reload 重新加載纔會生效;

本地虛擬域名配置(修改host)注意事項

可以配置域名轉發,但是一定要配置host,並且使host生效之後纔可以,設置完成之後要重啓瀏覽器

Linux 修改host

  • sudo vim /etc/hosts
  • 添加好對應的域名和ip,這樣訪問域名的時候不會訪問真實的域名
    例如 :
      10.211.55.6 image.misty.com 
        127.0.0.1  s.imisty.com

Windows

  • 進入 C:\windows\system32\drivers\etc

  • 用記事本打開hosts文件

  • 添加好對應的域名以及IP (和Linux環境下修改host一樣)

  • 保存退出

關於目錄訪問403 的問題

  • 實際配置過程中,將root目錄及其子目錄的路徑配置給nginx,訪問出現403 ,常規目錄可以

關於重定向過多的問題

  • 用nginx配置指向 本地的localhost:8080/talker ,出現重定向次數過多的情況,猜想是因爲層級關係導致,直接訪問8080端口即可,不需要直接訪問路徑下面的應用

啓動出錯:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

這個錯誤很常見,這次次居然是剛剛編譯安裝之後就出現了,以前是補充安裝模塊的時候纔會出現的,並且一旦出現以後重啓服務器或關閉停止nginx 的時候就會出現,這次詳細記錄一下
這個問題產生的原因是nginx的安裝目錄下沒有nginx.pid,但是我實際是有的,查閱資料發現,因爲每次重新啓動,系統都會自動刪除文件,所以解決方式就是更改pid文件存儲的位置,每次啓動的時候從固定的位置加載

  • 首先打開nginx配置文件註釋
    在這裏插入圖片描述
  • 再確定 ${nginx}/logs/nginx.pid是否存在
  • 如果不存在,${nginx}/sbin目錄執行一下./nginx -c /usr/local/nginx/conf/nginx.conf

其他相關命令

scp -R 目錄 [email protected]:~ 將制定的文件或者目錄上傳至服務器的用戶目錄

小確幸

每一絲靈感都值得被記錄,每一筆記錄都是成長,每一點成長都值得歡呼

博主個人站: www.imisty.cn
CSDN博客: https://blog.csdn.net/lookinthefog
博客園 :https://imist.cnblogs.com/

希望能夠認識一些熱愛技術的小夥伴,歡迎友鏈接喲

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