nginx打开网页直接下载文件的问题

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;
}

原因

看题主配置里面有laravel,看来是跟php有关,如果是php,那么题主你应该先去下载php5-fpm才对,因为nginx本身不像apache一样会执行php程序,而是交给php5-fpm执行.

所以,题主你的步骤应该如下 :

  • 下载php5-fpm

  • 配置nginx,使nginx跟fpm通信,网上有很多配置方法,我不重复,这里指提醒一点 : nginx跟fpm通信方式有两种,一个是通过ip,一个是通过socket.fpm跟nginx里面要配置成同一种通信方式!!

  • 最后测试是否成功.当然有可能到了这里还会出现访问页面下载下来的情况,如果遇到这个情况就需要再排查了,但是题主先搞定fpm比较稳妥.

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.

#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
这串代码我取消注释 nginx restart 会报错

nginx 访问localhost老是下载文件不能打开网页什么情况?的更多相关文章

  1. 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案

    访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案: 打开客戶端浏览器--工具---internet-安全-自定义级别-选择到低到中低. 然后点受信任站点,把你要访问的站点 ...

  2. 有哪些api接口可以实现微信自动唤醒浏览器,下载app,打开网页

    现在微信渠道可以说是拉新最快的渠道,因为微信具备强裂变性.但是目前微信对第三方下载链接的拦截是越来越严格了,那么想要在微信内肆无忌惮地推广链接就需要用到微信跳转浏览器的接口,那如何获取该接口呢?   ...

  3. nginx配置虚拟路径下载文件(.apk)

    公司将安卓apk文件放到服务器上,实现用户点击链接并下载 nginx version 1.14.1 nginx配置修改 server { listen 80; server_name localhos ...

  4. 在MFC中通过访问IP地址下载文件到本地

    void CDownLoad::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CDialogEx::OnOK(); UpdateData(TRUE); CStri ...

  5. asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)

    //请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...

  6. Winform中上传、下载文件选择打开文件的位置

    打开将要上传的文件 var fileName="";OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Mul ...

  7. (转nginx不浏览直接下载文件的解决方法

    原文:https://www.zhan200.com/xwt/39.html 如果nginx配置不对,就会造成部分文件,在浏览器中不是直接预览,而是进行了下载.修改的方法是修改配置文件.具体解决方法如 ...

  8. ionic实现下载文件并打开功能(file-transfer和file-opener2插件)

    作为一款app,下载文件功能,和打开文件功能,在某些场景下还是十分有必要的.使用cordova-plugin-file-transfer和cordova-plugin-file-opener2这两个插 ...

  9. 简单修改hosts文件加快打开网页速度

    这个电脑小技巧的帖子菲菲博客分享如何通过简单一招利用修改系统的hosts文件来实现有效加快浏览器打开网页的速度.尤其是网络繁忙时DNS服务器负担加重的时候效果特别明显,有兴趣就和菲菲一起来学习一下吧, ...

随机推荐

  1. 【Java】通过移除空行和注释来压缩 JavaScript 代码

    1. [代码]JavaScriptCompressor.java/** * This file is part of the Echo Web Application Framework (herei ...

  2. Oracle:ORA-09925 and linux Read-only file system error

    今天上午有同事反映应用数据库连接不上:于是排查数据库: [oracle@db ~]$ sqlplus / as sysdba SQL*Plus: Release - Production on Thu ...

  3. Redmine 数据库连接错误

    /******************************************************************** * Redmine 数据库连接错误 * 说明: * Open ...

  4. NSArray使用须知

    多用firstObject方法 在iOS7之前,我们获取NSArray的第一个元素,通常使用array[0],在iOS7中,新引入了公开的firstObject方法,对于空数组,该方法返回nil,而使 ...

  5. iOS设备闪光灯控制

    很多时候都需要在APP中控制闪光灯的开关状态,譬如扫描二维码.控制iOS设备的闪光灯代码非常简单,短短几行代码就可以搞定: AVCaptureDevice *device = [AVCaptureDe ...

  6. 理解iOS Event Handling

    写在前面 最近的一个iOS App项目中遇到了这么问题:通过App访问服务器的大多数资源不需要登录,但是访问某些资源是需要用户提供验证的,一般来说,通常App的做法(譬如美团App)将这些资源放在“我 ...

  7. 【NOI 2005】 维修数列

    [题目链接] 点击打开链接 [算法] 本题所运用的也是Splay的区间操作,但是实现较为困难 INSERT操作             将pos splay至根节点,pos+1 splay至根节点的右 ...

  8. 数据库备份脚本.sh

    #!/bin/bash #auto bakcup mysql db BAK_DIR=/data/backup/mysql/`date +%Y-%m-%d` MYSQL_DB=数据库名 MYSQL_PW ...

  9. OpenCV视频的读写

    实验室的项目是处理视频,所以就从视频的读取和写入开始吧! 常用的接口有C++和Python,Python肯定要简洁许多,不过因为项目需要,还是用C++了(PS:其实是我被Python的速度惊到了... ...

  10. GC 和 Full GC 有什么区别?

    GC(或Minor GC):收集 生命周期短的区域(Young area). Full GC (或Major GC):收集生命周期短的区域(Young area)和生命周期比较长的区域(Old are ...