阿里雲Centos伺服器安裝Nginx

CJTARRR發表於2024-03-25
  • 安裝依賴

    yum install openssl

    yum install zlib

    yum install pcre

    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • 安裝Nginx

    yum install nginx
  • 啟動Nginx/重啟Nginx

    service nginx start  # 啟動
    service nginx restart # 重啟
  • 驗證安裝

    在安全組設定中,開放伺服器80埠。
    瀏覽器訪問http://{伺服器ip},出現歡迎介面表示安裝且啟動成功。

    # 配置檔案路徑
    /etc/nginx/nginx.conf
    # 上面的檔案預設會引用/etc/nginx/conf.d資料夾下的所有結尾為.conf的配置檔案,其中該資料夾預設存在一個default.conf配置檔案。
  • 簡單使用

    • 轉發

      server {
      listen 80;
      server_name localhost;

      #access_log /var/log/nginx/host.access.log main;

      location / {
      # root /usr/share/nginx/html;
      # index index.html index.htm;
      proxy_pass http://127.0.0.1:5000; # 將服務端80埠接收的請求轉發到127.0.0.1:5000
      }

      #error_page 404 /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
      root /usr/share/nginx/html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      # proxy_pass http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      # root html;
      # fastcgi_pass 127.0.0.1:9000;
      # fastcgi_index index.php;
      # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      # include fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      # deny all;
      #}
      }
    • 負載均衡

      upstream myserver {
      ip_hash; # 負載均衡演算法,有多種可選,可以選擇多種均衡策略
      server 127.0.0.1:5000;
      server 127.0.0.1:5001;
      server 127.0.0.1:5002;
      }


      server {
      listen 80;
      server_name localhost;

      #access_log /var/log/nginx/host.access.log main;

      location / {
      proxy_pass http://myserver; # 將伺服器80埠接收的請求轉發到負載均衡組myserver中
      # root /usr/share/nginx/html;
      # index index.html index.htm;
      }

      #error_page 404 /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
      root /usr/share/nginx/html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      # proxy_pass http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      # root html;
      # fastcgi_pass 127.0.0.1:9000;
      # fastcgi_index index.php;
      # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      # include fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      # deny all;
      #}
      }

相關文章