如何玩转 nginx/1.10.3 (Ubuntu)

1、安装:

sudo apt-get install nginx

2、配置文件

 vi /etc/nginx/sites-enabled/default

server {
    listen 8080 default_server;             //我80端口已被用,所以设为8080
    listen [::]:8080 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/html2;   //网站目录           

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;  //这就是入口文件,会按顺序找

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

3、在/var/www/html2 目录下建立相应的文件

vi index.html

放入一些内容

这时,打开浏览器输入localhost:8080就可以看到入口文件的内容了(:,是不是很好用,很神奇?

4、不过默认是不能解析php文件的哦。

vi index.php

当输入http://localhost:8080/index.php时,会自动下载此文件。):


如何可以解析php呢??上网找吧。。。。。。

答案:http://www.linuxidc.com/Linux/2016-05/131154.htm



以下是当使用tp5框架时,nginx的配置,具体参数根据自身修改。

  server {
        listen 8080;
        server_name  guoshu.com;
        access_log    /var/www/html2/nginx.log;
        error_log    /var/www/html2/nginx_error.log;
        set        $root    /var/www/html2;
        location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
        {
            root $root;
        }
        location / {
            root    $root;
            index    index.html index.php;
            if ( -f $request_filename) {
                break;
            }
            if ( !-e $request_filename) {
                rewrite ^(.*)$ /index.php/$1 last;
                break;
            }
        }
        location ~ .+\.php($|/) {
            set $script $uri;
            set $path_info "";
            if ($uri ~ "^(.+\.php)(/.+)") {
                set $script $1;
                set $path_info $2;
            }
           # fastcgi_pass    unix:/run/php/php7.0-fpm.sock;
              fastcgi_pass     127.0.0.1:9000;   #如果是php5.5的,用这个可以
             fastcgi_index    index.php?IF_REWRITE=1;
            fastcgi_param    PATH_INFO    $path_info;
            fastcgi_param    SCRIPT_FILENAME    $root$fastcgi_script_name;
            fastcgi_param    SCRIPT_NAME    $script;
            include        fastcgi_params;
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值