Score:0

Nginx - virtualhost not working properly

gb flag

I bought macOS Big Sur and I'm trying to install php + nginx on it, but I have a problem all the time and can't add my domain correctly. I have this configuration:

usr/local/etc/php/7.3/php-fpm.d/www.conf

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or /usr/local/Cellar/[email protected]/7.3.31) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = pcyrkl01
group = staff

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9073

I try different configurations, but I can't find a solution to the problem, despite searching for several hours.

My virtualhost for admin.local

server {

        # For https
        listen 443 ssl;
        listen [::]:443 ssl;
        ssl_certificate /Users/pcyrkl01/ssl/default.crt;
        ssl_certificate_key /Users/pcyrkl01/ssl/default.key;

        server_name admin.local;
        root /Users/pcyrkl01/sites/admin.local/web_admin;
        index index.php index.html index.htm;

        location / {
          try_files $uri @rewriteapp;
        }

        location @rewriteapp {
            rewrite ^(.*)$ /app_dev.php/$1 last;
        }

        location ~ ^/(app|app_dev|config|index)\.php(/|$) {
            fastcgi_pass 127.0.0.1:9073;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fixes timeouts
            fastcgi_read_timeout 600;
            fastcgi_param HTTPS off;
        }

        location ~* \.(eot|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
        }

        error_log /Users/pcyrkl01/web_log/symfony_error.log;
        access_log /Users/pcyrkl01/web_log/symfony_access.log;
    }

local.backend

 server {
    
            # For https
            listen 443 ssl;
            listen [::]:443 ssl;
            ssl_certificate /Users/pcyrkl01/ssl/default.crt;
            ssl_certificate_key /Users/pcyrkl01/ssl/default.key;
    
            server_name admin.local;
            root /Users/pcyrkl01/sites/admin.local/web;
            index index.php index.html index.htm;
    
            location / {
              try_files $uri @rewriteapp;
            }
    
            location @rewriteapp {
                rewrite ^(.*)$ /app_dev.php/$1 last;
            }
    
            location ~ ^/(app|app_dev|config|index)\.php(/|$) {
                fastcgi_pass 127.0.0.1:9073;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fixes timeouts
                fastcgi_read_timeout 600;
                fastcgi_param HTTPS off;
            }
    
            location ~* \.(eot|ttf|woff|woff2)$ {
                add_header Access-Control-Allow-Origin *;
            }
    
            error_log /Users/pcyrkl01/web_log/symfony_error.log;
            access_log /Users/pcyrkl01/web_log/symfony_access.log;
        }

my nginx.conf

#user  nobody;
worker_processes  1;

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


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

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root html;
            fastcgi_intercept_errors on;
            fastcgi_pass   127.0.0.1:9073;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            # Again, you may need to change this path, it's based on a brew install.
            include /usr/local/etc/nginx/fastcgi_params;
        }

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

        # 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:9000
        #
        location ~ \.php$ {
           root           html;
            fastcgi_pass   127.0.0.1:9073;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    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 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

My file permission

 -rw-r--r--    1 pcyrkl01  staff   1.9K Oct 20 15:03 Dockerfile
-rw-r--r--    1 pcyrkl01  staff   2.3K Oct 20 15:03 Dockerfile73
-rw-r--r--    1 pcyrkl01  staff   2.2K Oct 20 15:03 README.md
drwxr-xr-x    9 pcyrkl01  staff   288B Oct 20 15:03 app
drwxr-xr-x    4 pcyrkl01  staff   128B Oct 20 15:15 bin
-rw-r--r--    1 pcyrkl01  staff   4.0K Oct 20 15:03 composer.json
-rw-r--r--    1 pcyrkl01  staff   345K Oct 20 15:03 composer.lock
-rwxr-xr-x    1 pcyrkl01  staff   1.8M Oct 20 15:03 composer.phar
drwxr-xr-x    3 pcyrkl01  staff    96B Oct 20 15:03 deploy
-rw-r--r--    1 pcyrkl01  staff   662B Oct 20 15:03 deploy.php
-rw-r--r--    1 pcyrkl01  staff   3.6K Oct 20 15:03 deploy_aws.php
-rw-r--r--    1 pcyrkl01  staff    12K Oct 20 15:03 deploy_test.php
-rw-r--r--    1 pcyrkl01  staff   787B Oct 20 15:03 docker-compose.dev.yml
drwxr-xr-x    5 pcyrkl01  staff   160B Oct 20 15:03 docs
drwxr-xr-x    6 pcyrkl01  staff   192B Oct 20 15:03 front
-rw-r--r--    1 pcyrkl01  staff    13K Oct 20 15:03 gulpfile.js
-rw-r--r--    1 pcyrkl01  staff   1.5K Oct 20 15:03 nagios.php
drwxr-xr-x  677 pcyrkl01  staff    21K Oct 20 15:12 node_modules
-rw-r--r--    1 pcyrkl01  staff   287K Oct 20 15:12 npm-shrinkwrap.json
-rw-r--r--    1 pcyrkl01  staff   290K Oct 20 15:03 package-lock.json
-rw-r--r--    1 pcyrkl01  staff   769B Oct 20 15:03 package.json
-rw-r--r--    1 pcyrkl01  staff   1.3K Oct 20 15:03 phpunit.xml.dist
drwxr-xr-x    5 pcyrkl01  staff   160B Oct 20 15:03 src
drwxr-xr-x    6 pcyrkl01  staff   192B Oct 20 15:03 tests
drwxr-xr-x    6 pcyrkl01  staff   192B Oct 20 15:15 var
drwxr-xr-x   65 pcyrkl01  staff   2.0K Oct 20 15:15 vendor
drwxr-xr-x   31 pcyrkl01  staff   992B Oct 20 15:15 web
drwxr-xr-x    7 pcyrkl01  staff   224B Oct 20 15:09 web_admin
drwxr-xr-x    6 pcyrkl01  staff   192B Oct 20 15:03 web_api

In /etc/hosts I have added: 127.0.0.1 admin.local local.backend

brew services list

nginx   started pcyrkl01 /Users/pcyrkl01/Library/LaunchAgents/homebrew.mxcl.nginx.plist
[email protected] started pcyrkl01 /Users/pcyrkl01/Library/LaunchAgents/[email protected]

The problem is that both hosts open to me instead of the site is the location /usr/local/var/www, instead of mine - Users/pcyrkl01/sites/local.admin/ web or web_admin

tw flag
Are you accessing the website on https? It looks like different host is used. You can see the path in the log is different from your `root /usr/local/var/www/localdomain;`
PawelC avatar
gb flag
@martin.malek made an update of the whole post and added more details :)
tw flag
still don't see what url you are accessing
PawelC avatar
gb flag
@martin.malek admin.local and local.backend with https on begin
tw flag
Looks ok. Just the nginx.conf is not whole. Are you including your server files in nginx.conf? Just try to make some error in local.backend file if nginx will not start.
PawelC avatar
gb flag
@martin.malek I added all the content of nginx.conf to the post
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.