如何使用PHP slim框架配置nginx.conf以使用友好的URL?

时间:2017-03-22 14:30:19

标签: php apache .htaccess nginx

我在本地网络服务器上有这个文件系统布局:

www
--mysite
---public_html
----index.php ( I use Slim here )
---app
----vendor
-----slim
------slim
-------Slim
--------....
----routes
----lib

在index.php中我使用Slim框架。 这是代码:

<?php
require '../app/vendor/autoload.php';
$app = new Slim\App();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();
?>

如何配置nginx.conf以使用url友好(使用斜杠)执行请求?
例如:

localhost/mysite/hello/Bob

这是我的nginx.conf:

#
# WPN-XM Server Stack
#
# Nginx Configuration File
#
# Notes:
#
# 1. Nginx is started with param "-p". This sets the root folder of the server as base path for Nginx.
# 2. At the end of this config all server configs from the "domains-enabled" folder are included.
# 3. The default PHP handler is "php" (see section "PHP handler" and upstream definitions).
#

#user  nobody;
worker_processes auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
    multi_accept        off;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    # configure temporary paths
    fastcgi_temp_path       temp/fastcgi;
    uwsgi_temp_path         temp/uwsgi;
    scgi_temp_path          temp/scgi;
    client_body_temp_path   temp/client-body 1 2;
    proxy_temp_path         temp/proxy;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    server_name_in_redirect off;
    server_tokens           off;

    server_names_hash_bucket_size 64; # added for longer domain names
    server_names_hash_max_size    512;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;

    # Size Limits
    client_body_buffer_size     64k;
    client_header_buffer_size   4k;
    client_max_body_size        8M;
    large_client_header_buffers 4 64k;

    # Timeouts
    client_body_timeout     10;
    client_header_timeout   10;
    keepalive_timeout       30;
    send_timeout            10;
    keepalive_requests      10;

    # FastCGI
    fastcgi_connect_timeout      60;
    fastcgi_send_timeout         120;
    fastcgi_read_timeout         300; # default: 60 secs; when step debugging with XDEBUG, you need to increase this value
    fastcgi_buffer_size          64k;
    fastcgi_buffers              4 64k;
    fastcgi_busy_buffers_size    128k;
    fastcgi_temp_file_write_size 128k;

    # output compression saves bandwidth
    # http://wiki.nginx.org/HttpGzipModule
    gzip                on;
    gzip_buffers        16 8k;
    gzip_comp_level     5;
    gzip_http_version   1.1;
    gzip_min_length     1000;
    gzip_proxied        any;
    gzip_types          text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary           on;

    # show all files and folders
    autoindex on;

    #
    # PHP handler
    #

    upstream php {
        server 127.0.0.1:9100;
    }

    upstream php_pool {
        server 127.0.0.1:9100 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9101 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9102 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:9103 weight=1 max_fails=3 fail_timeout=20s;
    }

    server {
        # access from localhost only
        listen       127.0.0.1:80;
        server_name  localhost;
        root         www;

        # the following default "catch-all" configuration, allows access to the server from outside.
        # please ensure your firewall allows access to tcp/port 80. check your "skype" config.
        listen       80;
        # server_name  _;

        log_not_found off;
        charset utf-8;

        access_log  logs/access.log  main;

        # handle files in the root path /www

        # nginx configuration
        # here location ...


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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9100
        #
        location ~ \.php$ {
            try_files      $uri =404;
            fastcgi_pass   php;
            fastcgi_index  index.php;
            #fastcgi_param  PHP_FCGI_MAX_REQUESTS 1000;
            #fastcgi_param  PHP_FCGI_CHILDREN 100;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param   REMOTE_ADDR $http_x_real_ip;
            include        fastcgi_params;
        }

        # add expire headers and speed up image access with a vary header
        location ~* ^.+.(gif|ico|jpg|jpeg|png|flv|swf|pdf|mp3|mp4|xml|txt|js|css)$ {
            expires 30d;
            add_header Vary Accept-Encoding;
        }

        # only allow these request methods
        if ($request_method !~ ^(GET|HEAD|POST)$ ){ return 405; }

        # deny access to .htaccess files (if Apache's document root concurs with nginx's one)
        # deny access to git & svn repositories
        location ~ /(\.ht|\.git|\.svn) {
            access_log off;
            log_not_found off;
            deny  all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 spdy ssl;
    #    server_name  localhost;
    #    root         www;
    #
    #    ssl                  on;
    #    ssl_certificate      ../../../bin/openssl/certs/cert.pem;
    #    ssl_certificate_key  ../../../bin/openssl/certs/cert.key;
    #
    #    ssl_session_timeout  5m;
    #    ssl_session_cache    shared:SSL:50m;
    #
    #    ssl_protocols              TLSv1.1 TLSv1.2;
    #    ssl_ciphers                HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #
    #    HSTS
    #    add_header Strict-Transport-Security max-age=15768000;
    #
    #    OCSP Stapling ---
    #    fetch OCSP records from URL in ssl_certificate and cache them
    #    ssl_stapling on;
    #    ssl_stapling_verify on;
    #
    #    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9100
    #    #
    #    location ~ \.php$ {
    #        try_files      $uri =404;
    #        fastcgi_param  HTTPS on;
    #        fastcgi_pass   php;
    #        fastcgi_index  index.php;
    #        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #        fastcgi_param  REMOTE_ADDR $http_x_real_ip;
    #        include        fastcgi_params;
    #    }
    #}

    # include config files of "enabled" domains
    include domains-enabled/*.conf;
}

在服务器块中,我必须插入位置块。它应该如何?

谢谢, 加埃塔诺

0 个答案:

没有答案