nginx 安裝配置及使用 啟動權限拒絕問題


安裝

yum install -y nginx

查看安裝的路徑

whereis nginx

可能會有所不同 需要根據自己的查看

執行目錄:/usr/sbin/nginx
模塊所在目錄:/usr/lib64/nginx/modules
配置所在目錄:/etc/nginx/
默認站點目錄:/usr/share/nginx/htm
主要配置文件:/etc/nginx/nginx.conf 指向:/etc/nginx/conf.d/default.conf
PID目錄:/var/run/nginx.pid
錯誤日志:/var/log/nginx/error.log
訪問日志:/var/log/nginx/access.log


查看nginx是否運行

systemctl status nginx.service

這個是正在運行

這個是沒有運行


查看版本

nginx -V


配置nginx

路徑:/etc/nginx/conf.d
在里面創建后綴為.conf的文件
文件格式為

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # 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 {
        }
    }



查看添加的文件是否正確

/usr/sbin/nginx -t

nginx命令

#啟動
systemctl start nginx.service
#結束
systemctl stop nginx.service
#重啟
systemctl restart nginx.service

nginx 啟動失敗,日志里面報錯信息如下:
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:**** failed (13: Permission denied)

權限拒絕,經檢查發現是開啟selinux 導致的。 直接關閉
getenforce 這個命令可以查看當前是否開啟了selinux 如果輸出 disabled 或 permissive 那就是關閉了

如果輸出 enforcing 那就是開啟了 selinux

1、臨時關閉selinux

setenforce 0 ##設置SELinux 成為permissive模式
setenforce 1 ##設置SELinux 成為enforcing模式

2、永久關閉selinux,

修改/etc/selinux/config 文件
將SELINUX=enforcing改為SELINUX=disabled
重啟機器即可


nginx.conf配置文件說明


#運行用戶
user www-data; 
#啟動進程,通常設置成和cpu的數量相等
worker_processes 1;
 
#全局錯誤日志及PID文件
error_log /var/log/nginx/error.log;
pid    /var/run/nginx.pid;
 
#工作模式及連接數上限
events {
  use  epoll;       #epoll是多路復用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能
  worker_connections 1024;#單個后台worker process進程的最大並發鏈接數
  # multi_accept on;
}
 
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
   #設定mime類型,類型由mime.type文件定義
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  # 日志格式設置
  #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  #                  '$status $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for"';
#用log_format指令設置日志格式后,需要用access_log來指定日志文件存放路徑
  access_log  /var/log/nginx/access.log;
 
  #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
  #必須設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
  sendfile    on;
  #tcp_nopush   on;
 
  #連接超時時間
  #keepalive_timeout 0;
  keepalive_timeout 65;
  tcp_nodelay    on;
   
  #開啟gzip壓縮
  gzip on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 
  #設定請求緩沖
  client_header_buffer_size  1k;
  large_client_header_buffers 4 4k;
 
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
 
  #設定負載均衡的服務器列表
   upstream mysvr {
  #weigth參數表示權值,權值越高被分配到的幾率越大
  #本機上的Squid開啟3128端口
  server 192.168.8.1:3128 weight=5;
  server 192.168.8.2:80 weight=1;
  server 192.168.8.3:80 weight=6;
  }
 
 
  server {
      #偵聽80端口
        listen    80;
        #定義使用www.xx.com訪問
        server_name www.xx.com;

        #設定本虛擬主機的訪問日志
        access_log logs/www.xx.com.access.log main;

      #默認請求
        location / {
         root  /root;   #定義服務器的默認網站根目錄位置
         index index.php index.html index.htm;  #定義首頁索引文件的名稱

         fastcgi_pass www.xx.com;
         fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
         include /etc/nginx/fastcgi_params;
         }

      # 定義錯誤提示頁面
         error_page  500 502 503 504 /50x.html;
         location = /50x.html {
         root  /root;
       }

      #靜態文件,nginx自己處理
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
              root /var/www/virtual/htdocs;
        #過期30天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
        expires 30d;
            }
      #PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
      location ~ \.php$ {  #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
        root /root;   #根目錄
        fastcgi_pass 127.0.0.1:9000; #請求轉向定義的服務器列表
        
        fastcgi_index index.php; # 如果請求的Fastcgi_index URI是以 / 結束的, 該指令設置的文件會被附加到URI的后面並保存在變量$fastcig_script_name中
        
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
      }
      #設定查看Nginx狀態的地址
      location /NginxStatus {
        stub_status      on;
        access_log       on;
        auth_basic       "NginxStatus";
        auth_basic_user_file conf/htpasswd;
      }
      #禁止訪問 .htxxx 文件
      location ~ /\.ht {
        deny all;
      }
    
   }
}

以上是一些基本的配置,使用Nginx最大的好處就是負載均衡如果要使用負載均衡的話,可以修改配置http節點如下:



#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
   #設定mime類型,類型由mime.type文件定義
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;
  #設定日志格式
  access_log  /var/log/nginx/access.log;
 
  #省略上文有的一些配置節點
 
  #。。。。。。。。。。
 
  #設定負載均衡的服務器列表
   upstream mysvr {
  #weigth參數表示權值,權值越高被分配到的幾率越大
  server 192.168.8.1x:3128 weight=5;#本機上的Squid開啟3128端口
  server 192.168.8.2x:80 weight=1;
  server 192.168.8.3x:80 weight=6;
  }
 
  upstream mysvr2 {
  #weigth參數表示權值,權值越高被分配到的幾率越大
 
  server 192.168.8.x:80 weight=1;
  server 192.168.8.x:80 weight=6;
  }
 
  #第一個虛擬服務器
  server {
  #偵聽192.168.8.x的80端口
    listen    80;
    server_name 192.168.8.x;
 
   #對aspx后綴的進行負載均衡請求
  location ~ .*\.aspx$ {
 
     root  /root;   #定義服務器的默認網站根目錄位置
     index index.php index.html index.htm;  #定義首頁索引文件的名稱
 
     proxy_pass http://mysvr ;#請求轉向mysvr 定義的服務器列表
 
     #以下是一些反向代理的配置可刪除.
 
     proxy_redirect off;
 
     #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     client_max_body_size 10m;  #允許客戶端請求的最大單文件字節數
     client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數,
     proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時)
     proxy_send_timeout 90;    #后端服務器數據回傳時間(代理發送超時)
     proxy_read_timeout 90;     #連接成功后,后端服務器響應時間(代理接收超時)
     proxy_buffer_size 4k;       #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小
     proxy_buffers 4 32k;        #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置
     proxy_busy_buffers_size 64k;  #高負荷下緩沖大小(proxy_buffers*2)
     proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
 
    }
 
   }
}

nginx文檔

nginx文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM