Nginx 1.10.3+PHP5.6配置REST风格URL重定向求助
Hey there! Let's work through this RESTful URL rewrite problem step by step. Since your nginx.conf doesn't have a server block, that's totally normal for Dockerized Nginx setups—often server configurations are split into separate files in dedicated directories instead of being directly in the main config. Here's exactly what you need to do:
Step 1: Locate the right config directory in your Docker container
First, hop into your running container to find where Nginx looks for server-specific configs. Run this command (replace <your-container-name> with your actual container name or ID):
docker exec -it <your-container-name> bash
For the 1and1internet/ubuntu-16-nginx-php-5.6 image, Nginx typically loads configs from either:
/etc/nginx/conf.d/(files ending with.confare auto-loaded)/etc/nginx/sites-available/(with symlinks to/etc/nginx/sites-enabled/for active sites)
Check these directories to see which one your setup uses—most likely /etc/nginx/conf.d/.
Step 2: Create or edit a server block configuration
If there's no existing config file in /etc/nginx/conf.d/, create a new one (e.g., default.conf). If one exists, edit it to add the following server block. Make sure to adjust paths to match your actual project structure:
server { listen 80; server_name localhost; root /var/www/html; # Update this to your PHP project's root directory in the container index index.php index.html index.htm; # Rewrite rule for REST-style URLs location /load { # Maps http://localhost/load/accounts to http://localhost/load.php?process=accounts rewrite ^/load/([^/]+)$ /load.php?process=$1 last; } # Handle PHP file execution (critical for your Zend Engine setup) location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; # Match your PHP-FPM socket path fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Quick checks for accuracy:
- Confirm the
rootpath: In the1and1internet/ubuntu-16-nginx-php-5.6image, the default project root is usually/var/www/html—verify this by checking the container's filesystem. - Verify the
fastcgi_passpath: Check/etc/php/5.6/fpm/pool.d/www.conffor thelistendirective (it should point to the socket path used above).
Step 3: Validate and reload Nginx
Before applying changes, test the config for syntax errors:
nginx -t
If you see test is successful, you're good to go. Reload Nginx to apply the new config (this avoids downtime):
nginx -s reload
Step 4: Test your RESTful URL
To confirm everything works, add a quick test to your load.php file:
<?php echo "Process parameter: " . $_GET['process']; ?>
Visit http://localhost/load/accounts in your browser—you should see "Process parameter: accounts" if the rewrite is working correctly.
Bonus: Persist config changes (if using Docker Compose)
If you're running the container with Docker Compose, mount your local config file and project directory to avoid losing changes when the container restarts. Add this to your docker-compose.yml:
services: web: image: 1and1internet/ubuntu-16-nginx-php-5.6 volumes: - ./local-conf/default.conf:/etc/nginx/conf.d/default.conf - ./your-project-folder:/var/www/html ports: - "80:80"
内容的提问来源于stack exchange,提问作者Brian Hochgurtel

