Centos7的Nginx配置


1 Centos7上Nginx配置

1.1 nginx位置

通過whereis 命令查詢可得

[fanbi@VM_0_12_centos ~]$ whereis nginx
nginx: /usr/bin/nginx /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
[fanbi@VM_0_12_centos ~]$

  • /etc/nginx是其安裝目錄

  • /usr/sbin/nginx是其運行程序

  • usr/share/nginx是默認讀取位置,若沒其它配置,則讀取其下面的index.html文件

  • 具體配置和寶塔Linux面板上Nginx配置是一樣的

2 寶塔Linux面板上Nginx配置

2.1 server

2.1.1 HTTP

   listen 80;
      server_name unionline.top;
      index index.php index.html index.htm default.php default.htm default.html;
      root /www/wwwroot/unionline.top;
      
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off; 
    }
    access_log  /www/wwwlogs/unionline.top.log;
    error_log  /www/wwwlogs/unionline.top.error.log;

2.1.2 HTTPS

# HTTPS server
server {
  listen       443;
  server_name  unionline.top;
  root   /www/wwwroot/unionline.top;
  index  index.html index.htm;
  
  ssl on;

  ssl_certificate      1_www.unionline.top_bundle.crt;
  ssl_certificate_key  2_www.unionline.top.key;

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

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
  ssl_prefer_server_ciphers on;
  
  #ERROR-PAGE-START  錯誤頁配置,可以注釋、刪除或修改
  error_page 404 /404.html;
  error_page 502 /502.html;
  #ERROR-PAGE-END
  
  #PHP-INFO-START  PHP引用配置,可以注釋或修改
  include enable-php-56.conf;
  #PHP-INFO-END

	#默認是blog為主頁
  location / {
          proxy_pass http://182.254.111.234:4000/;
 			#root /your_project_dir/blog/myHexoBlog/public;
          location ~ .*\.(css|js)$ {
             root /your_project_dir/blog/myHexoBlog/public;
             if (-f $request_filename) {
                  expires 1d;
                  break;
                  }
              }
      }

 

 location /blog/ {
          proxy_pass https://182.254.111.234:4000/;
      }

 location /html/ {
         	root /www/wwwroot/unionline.top/;
  		index index.html ;
  
         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|eot|svg|ttf|woff|woff2)$ {
              root /www/wwwroot/unionline.top/;
             if (-f $request_filename) {
                  expires 1d;
                  break;
                  }
              }
      }
}

2.1.3 重定向

  {
      listen 80;
      large_client_header_buffers 4 128k;
  	  server_name www.unionline.top;

      location / {
      #開啟對http1.1支持
      proxy_http_version 1.1;
      
      #設置Connection為空串,以禁止傳遞頭部到后端
      #http1.0中默認值Connection: close
      proxy_set_header Connection "";
      
      proxy_pass http://unionline.top;
    }

2.1.4 http跳轉https

server
{
  # 80端口是http正常訪問的接口
  listen 80;
  server_name _;
  # 在這里,我做了https全加密處理,在訪問http的時候自動跳轉到https
  rewrite ^(.*)$ https://$host$1 permanent; #這行是關鍵
}

3.2 http

4 問題

4.1 Nginx 一個服務器多域名配置 以及 訪問php文件直接下載而不運行

include enable-php.conf; 這句話起到了關鍵作用


location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

4.2 403錯誤

通過查看對應的error.log,主要原因有

  1. 項目路徑無index.html index.php等等文件
  2. 項目js/css等路徑不正確
  3. 權限不夠 (nginx failed (13: Permission denied))
    • /www/server/nginx/conf/nginx.conf 中user擁有者和組別分別是www www,而項目是root或者fanbi的擁有着,改成user root root即可
    • 通過寶塔面板新建的項目默認用戶是www,組別是www

5 參考

1. 四種解決Nginx出現403 forbidden 報錯的方法


免責聲明!

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



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