Nginx

502 壞網關 nginx 和 apache 伺服器

  • June 1, 2020

您好,您能幫我在我的 ubuntu 16.04 伺服器上設置 apache 和 nginx 伺服器,但是當我嘗試訪問 localhost/info.php 範例時,我得到了

502 錯誤網關 nginx/1.9.15 (Ubuntu)

錯誤 502 bad gateway,這是來自錯誤日誌文件:

2017/01/24 10:50:55

$$ error $$14774#14774:*29 連接()失敗(111:連接被拒絕),同時連接到上游,客戶端:127.0.0.1,伺服器:example.com,請求:“GET /info.php HTTP/1.1”,上游:“ http ://127.0.0.1:8080/info.php ”,主機:“本地主機”

我的 /etc/apache2/ports.conf 是:

Listen 8000

<IfModule ssl_module>
   Listen 443
</IfModule>

<IfModule mod_gnutls.c>
   Listen 443
</IfModule>

我的 /etc/nginx/sites-available/default 文件是:

server {
   listen 80 default_server;
   listen [::]:80 default_server;

   root /var/www/html;

   # Add index.php to the list if you are using PHP
   index index.php index.html index.htm index.nginx-debian.html;

   server_name 127.0.0.1;

   location / {
       # First attempt to serve request as file, then
       # as directory, then fall back to displaying a 404.
       try_files $uri $uri/ =404;
       proxy_pass http://127.0.0.1:8000;
           include /etc/nginx/proxy_params;
   }

   location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
       expires      30d;
       }

   }

      location @proxy {
               proxy_pass http://127.0.0.1:8000;
               include /etc/nginx/proxy_params;
      }
      location ~* \.php$ {
               proxy_pass http://127.0.0.1:8000;
               include /etc/nginx/proxy_params;
      }
}

我的 /etc/nginx/nginx.conf 文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
   worker_connections 768;
   # multi_accept on;
}

http {

   ##
   # Basic Settings
   ##

   sendfile on;
   tcp_nopush on;
   tcp_nodelay on;
   keepalive_timeout 65;
   types_hash_max_size 2048;
   # server_tokens off;

   # server_names_hash_bucket_size 64;
   # server_name_in_redirect off;

   include /etc/nginx/mime.types;
   default_type application/octet-stream;

   ##
   # SSL Settings
   ##

   ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
   ssl_prefer_server_ciphers on;

   ##
   # Logging Settings
   ##

   access_log /var/log/nginx/access.log;
   error_log /var/log/nginx/error.log;

   ##
   # Gzip Settings
   ##

   gzip on;
   gzip_disable "msie6";

   # gzip_vary on;
   # gzip_proxied any;
   # gzip_comp_level 6;
   # gzip_buffers 16 8k;
   # gzip_http_version 1.1;
   # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

   ##
   # Virtual Host Configs
   ##

   include /etc/nginx/conf.d/*.conf;
   include /etc/nginx/sites-enabled/*;
}

/var/www/html 文件夾的權利和所有權:

➜  html sudo ls -lah /var/www/html          
общо 40K
drwx--s--x  6 dimitar www-data 4,0K яну 24 10:44 .
drwxr-sr-x  3 dimitar www-data 4,0K дек 28 12:26 ..
-rw-r-Sr--  1 dimitar www-data  12K яну 24 00:52 index.html
-rw-r--r--  1 dimitar www-data   18 яну 24 10:44 info.php
drwxrwsr-x  6 dimitar www-data 4,0K яну 10 14:02 Laracast
drwxrwsr-x  2 dimitar www-data 4,0K яну 23 20:17 phpmyadmin
drwxrwxrwx  5 dimitar www-data 4,0K дек 16 11:28 softuni
drwxrwsr-x 12 dimitar www-data 4,0K яну  3 20:04 users

sudo netstat -lntp 的輸出:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:7845            0.0.0.0:*               LISTEN      5054/skype      
tcp        0      0 127.0.0.1:30666         0.0.0.0:*               LISTEN      31454/Viber     
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2521/mysqld     
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      2523/smbd       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14328/nginx -g daem
tcp        0      0 127.0.0.1:4371          0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 127.0.0.1:5940          0.0.0.0:*               LISTEN      2301/teamviewerd
tcp        0      0 0.0.0.0:57621           0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 127.0.0.1:45112         0.0.0.0:*               LISTEN      31454/Viber     
tcp        0      0 0.0.0.0:220             0.0.0.0:*               LISTEN      1233/sshd       
tcp        0      0 127.0.0.1:4381          0.0.0.0:*               LISTEN      28075/spotify   
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      2523/smbd       
tcp6       0      0 :::139                  :::*                    LISTEN      2523/smbd       
tcp6       0      0 :::220                  :::*                    LISTEN      1233/sshd       
tcp6       0      0 :::445                  :::*                    LISTEN      2523/smbd       
tcp6       0      0 :::8000                 :::*                    LISTEN      15002/apache2  

/etc/nginx/proxy_params 的內容:

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

client_max_body_size 100M;
client_body_buffer_size 1m;
proxy_intercept_errors on;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 256 16k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 0;
proxy_read_timeout 300;

有人可以幫我一把嗎?

兩天后,我終於設法解決了我的問題。感謝這個頁面

我最終刪除了該/etc/nginx/sites-available/default文件,並且只有一個 apache 文件。

只需按照頁面中的說明輸入您的 IP 地址,127.0.0.1如果您只是嘗試為您的本地環境設置它,您可以輸入。

您也可以跳過 8 部分並按照其他部分進行,因為您不需要它們。這些用於設置,以便您的伺服器/com 可以在我們的網路之外可見,以便呼叫 - 網際網路 :)

引用自:https://serverfault.com/questions/828218