nginx rewriting with drupal windows

2019-08-07 15:06发布

问题:

this is the first time i use nginx , and i have two problems with it, the first one is that i want to redirect http://localhost/project automatically to http://localhot/project/en/ and en is subdirectory on project.

The second problem : in localhost/project/en/ i can see the index.php i mean the main page but every redirecting from the project give me 404 not found like localhost/project/en/people/ or localhost/project/en/people/article1

NB : i use nginx in windows with drupal and . here is my configuration:

    worker_processes  1;
events {
    worker_connections  1024;
}
http {
    server_tokens       on;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    ssi         off;

    #Timeouts
        client_body_timeout 5;
        client_header_timeout   5;
        keepalive_timeout   25 25;
        send_timeout        15s;
    resolver_timeout    3s;

    #Directive sets timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds. 
    fastcgi_connect_timeout 5s;

    #Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate. 
    fastcgi_read_timeout    400s;

    #Directive specifies request timeout to the server. The timeout is calculated between two write operations, not for the whole request. If no data have been written during this period then serve closes the connection.
    fastcgi_send_timeout    150s;

    fastcgi_buffers 8 32k;
    fastcgi_buffer_size 32k;
    #fastcgi_busy_buffers_size 256k;
    #fastcgi_temp_file_write_size 256k;

    open_file_cache off;

    #php max upload limit cannot be larger than this       
    client_max_body_size 8m;
        ####client_body_buffer_size  1K;
        client_header_buffer_size 1k;
        large_client_header_buffers 2 1k;   
    types_hash_max_size 2048;

    include nginx.mimetypes.conf;
    default_type text/html;

    ##
    # Logging Settings
    ##
    access_log "c:/wt-nmp/log/nginx_access.log";
    error_log "c:/wt-nmp/log/nginx_error.log" warn; #debug or warn
    log_not_found on;  #enables or disables messages in error_log about files not found on disk. 
    rewrite_log off;

    #Leave this off
    fastcgi_intercept_errors off;

    gzip  off;

    index  index.php index.htm index.html;

    server {
        listen      127.0.0.1:80    default_server;
        #listen     [::1]:80    ipv6only=on;
        server_name  localhost;

        root "c:/wt-nmp/www/";
        autoindex on;

        allow       127.0.0.1;
        #allow      ::1;
        deny        all;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
           # deny  all;   
        }

    location /project {
    index index.php;
    try_files $uri $uri/ @handler;
    }

    location @handler { rewrite / /project/en/index.php; }

    location ~ ^/en/ {
         root "c:/wt-nmp/www/project";
        try_files $uri $uri/ /en/index.php?$args;
    }
        #tools are now served from wt-nmp/include/tools/
        location ~ ^/tools/.*\.php$ {                   
            root "c:/wt-nmp/include";
            try_files $uri =404; 
            include     nginx.fastcgi.conf;
            fastcgi_pass    php_farm;
        }
        location ~ ^/tools/ {
            root "c:/wt-nmp/include";
        }

        location ~ \.php$ {
           # try_files $uri =404; 
               # fastcgi_pass   php_farm;
            fastcgi_pass     127.0.0.1:9000;
            include     nginx.fastcgi.conf;
            }

    }

    include domains.d/*.conf;

    include nginx.phpfarm.conf;
}

回答1:

WPN-XM Server Stack Installation Instructions for Drupal 7 with "Clean URLs"

Installation Steps:

  1. Download http://ftp.drupal.org/files/projects/drupal-7.34.zip
  2. Extract into c:\wpn-xm\www
  3. rename versionized folder to just "drupal" = full path to drupal = c:\wpn-xm\www\drupal
  4. run drupal install - http://localhost/drupal/install.php
  5. activate missing PHP extensions: maybe gd2, mbstring, then restart php
  6. reload install page, all green, proceed to database dialog
  7. before filling the database dialog, create database "drupal" in adminer, then use that as db in dialog
  8. proceed with install steps until done

Browse: http://localhost/drupal/ Ok, you just installed Drupal on localhost.


URL Rewriting and Clean URLs Steps:

After the installation, 3 things are needed to get short URLs working:

  1. provide new host "http://drupal.dev"
  2. add a Nginx Configuration for URL rewriting
  3. enabling "Clean URLs" in the Drupal Configuration

Important Notice

The short/nice/clean URLs work only for URLs starting with "http://drupal.dev/" not for "localhost". Using megaphone: not working with "http://localhost/..." - use "http://drupal.dev/...".


  1. Add "drupal.dev" to hosts file

You need to add "drupal.dev" to your "hosts" file.

  • manually or
  • via the WPN-XM Server Control Panel - Steps:
    • right-click tray icon - "Manage Hosts"
    • "Add" - Data "127.0.01" "drupal.dev"
    • Click Ok.
    • The Windows Permissions Dialog pops up.
    • Click Ok, to allow writing to the "hosts" file.
    • If Anti-Virus tool blocks writing to the hosts file, disable AV it, repeat the steps, enable it again.

Check: http://drupal.dev/?q=admin - ok

-

  1. **Add a Nginx server block for Drupal7 **

Use the following Nginx config file to activate rewriting URLs:

https://github.com/WPN-XM/software/blob/master/nginx/config/conf/domains-disabled/drupal7.conf

You might use an include directive in your main nginx.conf to load it.

Restart or rehash Nginx to activate the new configuration.

You can now start using the URL "http://drupal.dev" in your browser.

Important is the the following directive in your server block

# Make site accessible from http://drupal.dev/
server_name drupal.dev;

and the rewrite rule rewrite ^/(.*)$ /index.php?q=$1;.

Check: http://drupal.dev/admin


  1. Activate "Clean URLs" in Drupal Configuration

    • Testing "Clean URLs"
      • http://drupal.dev/admin/config/search/clean-urls/check
        • Response: {"status":true}
    • Enabling "Clean URLs" (b)
      • http://drupal.dev/admin/config/search/clean-urls
        • Check Checkbox & Save.

Links in the menu should appear in "Clean URL" form without "?q=".

Final Checks:

  • Hover links in the menu and check for short URLs. - ok
  • http://drupal.dev/admin - ok
  • http://drupal.dev/user/login - ok

Done.