What happens when nginx always downloads files and cannot open web pages when accessing localhost?
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 17:14:59
0
3
875

The default in the nginx sites-available file has modified the root path. But when accessing localhost, it always downloads the web page directly instead of opening the URL, which is very strange.

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梦

reply all(3)
巴扎黑

Looking at the configuration of the question, there is laravel, it seems to be related to PHP. If it is PHP, then the questioner, you should download php5-fpm first, because nginx itself does not execute PHP programs like Apache, but gives it to php5-fpm execution.

So, your steps should be as follows:

  • Download php5-fpm

  • Configure nginx to communicate with fpm. There are many configuration methods on the Internet. I will not repeat them. Here is a reminder: There are two ways for nginx to communicate with fpm, one is through ip, and the other is through socket.fpm and nginx. Configure the same communication method!!

  • Whether the final test was successful? Of course, it is possible that the accessed page will be downloaded when you get here. If you encounter this situation, you need to check again, but it is safer for the questioner to get the fpm first.

In order to better solve the problem of the subject, I saved a copy of the configuration I just made in the ubuntu14.04 environment

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

Not many changes:

  • index I put index.php in the first position

  • root path, please note that the last path does not have /

  • Remove the comments related to php, my /etc/php5/fpm/pool.d/www.conf中找到listen = /var/run/php5-fpm.sock,说明fpm是开启了socket,所以nginx的fastcgi_passparameter in fpm is also socket.

仅有的幸福

For example, at the end of php, you need to use php’s cgi to parse it

小葫芦
#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 会报错

@chenjiayao

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template