You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需指定端口在Nginx上运行Symfony PHP项目?

解决Docker部署Symfony项目无需指定端口访问的问题

问题描述

我使用Docker部署基于Symfony框架的PHP项目,包含Nginx、PHP和PostgreSQL服务。该项目之前在其他电脑上可无需指定端口通过http://bp.project访问,但当前电脑仅能通过http://bp.project:82访问,直接访问http://bp.project则失败。

当前配置

Nginx配置(nginx.conf)

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name bp.project;
    root /var/www/html/public/;
    index index.php;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;

        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            # Om nom nom cookies
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            # Custom headers and headers various browsers *should* be OK with but aren't
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            # Tell client that this pre-flight info is valid for 20 days
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            add_header Access-Control-Allow-Headers "Authorization";
            add_header Access-Control-Allow-Credentials "true";
            return 204;
        }
        if ($request_method = 'PUT') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        if ($request_method = 'PATCH') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        if ($request_method = 'DELETE') {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
    }

    location ~ /\.ht {
        deny all;
    }

    # location /assets/ {
    #
    # }
}

Docker Compose配置(docker-compose.yml)

version: '3'

services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./:/var/www/html/:delegated
      - ./.docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./.docker/nginx/sites/:/etc/nginx/sites-available
      - ./.docker/nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php
    networks:
      bp_project_network:
        aliases:
          - bp.project
    ports:
      - "82:80"

  php:
    image: thecodingmachine/php:8.0-v4-fpm
    volumes:
      - ./:/var/www/html/:delegated
    depends_on:
      - database
    environment:
      PHP_EXTENSION_INTL: 1
      PHP_EXTENSION_XDEBUG: 1
      PHP_INI_XDEBUG__MODE: "debug,develop"
      PHP_INI_XDEBUG__CLIENT_PORT: 10000
      PHP_INI_XDEBUG__IDEKEY: PHPSTORM
      PHP_INI_XDEBUG__START_WITH_REQUEST: trigger
      PHP_IDE_CONFIG: "serverName=bp.project"
      PHP_EXTENSION_PGSQL: 1
      PHP_EXTENSION_PDO_PGSQL: 1
      PHP_EXTENSION_MYSQLI: 0
    #      STARTUP_COMMAND_1: composer install
    #      STARTUP_COMMAND_2: php bin/console do:mi:m --no-interaction
    #      STARTUP_COMMAND_3: php bin/console do:f:l --group=default --no-interaction
    networks:
      - bp_project_network

  database:
    image: dockette/postgres:10
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=bp_project
    volumes:
      - database-volume:/var/lib/postgresql/data
    ports:
      - "5421:5432"
    networks:
      - bp_project_network

  adminer:
    image: dockette/adminer:dg
    ports:
      - "1112:80"
    networks:
      - bp_project_network

volumes:
  ###> doctrine/doctrine-bundle ###
  database-volume:
###< doctrine/doctrine-bundle ###

networks:
  bp_project_network:
    driver: bridge

解决方案

  • 修改Docker Compose端口映射:将Nginx服务的ports配置从"82:80"改为"80:80"。浏览器访问HTTP协议时默认使用80端口,不指定端口会自动请求80端口,将容器80端口映射到主机80端口后,即可直接通过http://bp.project访问。
    ports:
      - "80:80"
    
  • 检查并释放80端口占用:如果主机80端口被本地Nginx、Apache、IIS等服务占用,需先停止这些服务。
    • Windows:打开命令提示符执行netstat -ano | findstr ":80"找到进程ID,再用taskkill /PID <进程ID> /F终止进程。
    • Linux/macOS:执行sudo lsof -i :80找到进程,再用sudo kill -9 <进程ID>终止进程。
  • 确认本地hosts文件配置:确保bp.project已映射到127.0.0.1。
    • Windows:编辑C:\Windows\System32\drivers\etc\hosts,添加一行127.0.0.1 bp.project。
    • Linux/macOS:编辑/etc/hosts,添加一行127.0.0.1 bp.project。
  • 重启Docker服务:修改配置后,执行docker-compose down && docker-compose up -d重启所有服务,使配置生效。

内容的提问来源于stack exchange,提问作者Tchiggy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 10:43:13