首頁  >  問答  >  主體

nginx 訪問localhost老是下載檔案不能開啟網頁什麼狀況?

nginx sites-available檔案裡的default已經修改過root 路徑了。 但是造訪localhost的時候總是直接下載網頁而不是開啟網址 很奇怪。

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html/laravel/public;

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

    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;
    }
过去多啦不再A梦过去多啦不再A梦2713 天前825

全部回覆(3)我來回復

  • 巴扎黑

    巴扎黑2017-05-16 17:16:59

    看題主配置裡面有laravel,看來是跟php有關,如果是php,那麼題主你應該先去下載php5-fpm才對,因為nginx本身不像apache一樣會執行php程式,而是交給php5-fpm執行.

    所以,題主你的步驟應該如下 :

    • 下載php5-fpm

    • 配置nginx,使nginx跟fpm通信,網上有很多配置方法,我不重複,這裡指提醒一點: nginx跟fpm通信方式有兩種,一個是通過ip,一個是通過socket.fpm跟nginx裡面要配置成同一種通訊方式!!

    • 最後測試是否成功.當然有可能到了這裡還會出現訪問頁面下載下來的情況,如果遇到這個情況就需要再排查了,但是題主先搞定fpm比較穩.

    為了更好的解決題主的問題,我扑出一份我剛剛在ubuntu14.04環境下的配置

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
    
            root /var/www/html;
            index index.php index.html index.htm;
    
            # Make site accessible from http://localhost/
            server_name localhost;
    
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }
    
            # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
            #location /RequestDenied {
            #       proxy_pass http://127.0.0.1:8080;    
            #}
    
            #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;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
            ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
                    # With php5-cgi alone:
            #       fastcgi_pass 127.0.0.1:9000;
            #       # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #       deny all;
            #}
    }
    

    改動的地方不多 :

    • index我把index.php放在第一位置

    • root路徑,這裡注意最後路徑不要有 /

    • 去掉跟php有關的註解,我在fpm的 /etc/php5/fpm/pool.d/www.conf中找到listen = /var/run/php5-fpm.sock,说明fpm是开启了socket,所以nginx的fastcgi_pass參數也是socket.

    回覆
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 17:16:59

    譬如php結尾,你要用php的cgi去解析

    回覆
    0
  • 高洛峰

    高洛峰2017-05-16 17:16:59

    雷雷

    @陳家耀

    回覆
    0
  • 取消回覆