Symfony + Nginx + Proxy_pass +子目录:如何重写url?

时间:2019-02-12 13:26:49

标签: php symfony nginx url-rewriting reverse-proxy

作为程序员,我试图走出自己的舒适区,并尝试通过网络服务器进行完整的设置。我可以使用apache来做,但是,我需要使用proxy_pass,因为我有一些golang API。做一些研究,我意识到如果我还为我的Symfony应用程序设置代理,我会得到更好的基准测试器:

但是,要运行此symfony应用,我需要使用一个子目录。到目前为止,我设法通过ngnix上的proxy_pass运行golang和symfony,但是,Symfony尚未重写路由:

因此,当我访问http://myip/MYSUBDIR/app_dev.php/myroute时,所有(后台)加载的url(用于文件,脚本,ajax请求等)都像http://myip/app_dev.php/myroute

当然,我需要在网址上输入'MYSUBDIR'!

如何重写网址以解决我的问题?

我正在vps,php fpm 7.3和nginx / 1.14.0(Ubuntu)上运行Ubuntu 18.04

Symfony在v3.4上

/ etc / nginx / sites-avaliable / default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {

        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    location /MYSUBDIR/ {



        proxy_pass http://127.0.0.1:8080/;
        proxy_cache mysubdir_cache_setup;
        proxy_cache_key "$scheme://$host$request_uri";
        proxy_cache_lock on;
        proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
        add_header X-Cache $upstream_cache_status;

        error_log /var/log/nginx/mysubdir_cache_error.log;
        access_log /var/log/nginx/mysubdir_cache_access.log;
    }

    location /phpmyadmin {
            index index.php index.html index.htm;
            root /usr/share;
    }

    location /goapi/ {

            proxy_pass http://127.0.0.1:9003/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_cache_bypass $http_upgrade;
     }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny all;
    }
}

和#/ etc / nginx / sites-available / mysubdir

server {


    listen 8080;

    server_name localhost 127.0.0.1;

    root /var/www/html/mysubdir/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
       # When you are using symlinks to link the document root to the
       # current version of your application, you should pass the real
       # application path instead of the path to the symlink to PHP
       # FPM.
       # Otherwise, PHP's OPcache may not properly detect changes to
       # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
       # for more information).
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       # Prevents URIs that include the front controller. This will 404:
       # http://domain.tld/app.php/some-path
       # Remove the internal directive to allow URIs like this
       internal;
   }

   # return 404 for all other php files not matching the front controller
   # this prevents access to other php files you don't want to be accessible.
   location ~ \.php$ {
     return 404;
   }

   error_log /var/log/nginx/mysubdr_error.log;
   access_log /var/log/nginx/mysubdr_access.log;
}

我肯定两个文件都已在../ sites-enabled /

上正确链接

phpmydmin和goapi位置运行正常

请,我该如何解决?!

0 个答案:

没有答案