首页  >  问答  >  正文

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 天前832

全部回复(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
  • 取消回复