Nginx не заботится о новом виртуальном хосте - PullRequest
0 голосов
/ 03 февраля 2020

Я столкнулся с несколькими проблемами с nginx относительно моего vhost.

Когда я установил свой сервер nginx, я создал виртуальный хост, который в настоящее время работает на порту 8080. Не на 80, потому что он кажется, что-то уже использует его.

Недавно я пытался установить новый vhost, но nginx это не волнует.

Я построил свой vhost в /etc/nginx/sites-available. Я создал символическую ссылку в /etc/nginx/sites-enabled, которая является ссылкой на мой vhost.

Я перезапустил и перезагрузил, но nginx всегда перенаправляет меня на vhost по умолчанию.

Я пробовал следующие попытки:

  • I chmod to 775 символическая ссылка, а также vhost.
  • Я поместил vhost непосредственно в /etc/nginx/sites-enabled
  • Я поместил тело vhost непосредственно в nginx.conf
  • Я удалил символическую ссылку vhost по умолчанию

Всегда с перезагрузкой и перезагрузкой, но nginx продолжайте перенаправлять меня на vhost по умолчанию, даже если символическая ссылка не существует ...

Мне действительно нужна помощь, потому что я начал терять мой разум ^^. У меня есть полный доступ к серверу, поэтому я могу подсказать некоторые отзывы, которые могут вам помочь;).

С уважением,

BobHearn

Редактировать:

В следующем коде vhost1 работает нормально, vhost2 всегда показывает мне тело vhost по умолчанию.

nginx .conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

/ etc / nginx / sites-enabled /

/etc/nginx/sites-enabled$ ls -als
total 8
4 drwxr-xr-x 2 root root 4096 Feb  3 21:00 .
4 drwxr-xr-x 8 root root 4096 Feb  3 14:26 ..
0 lrwxrwxrwx 1 root root   26 Jan 25 17:47 vhost1 -> ../sites-available/vhost1
0 lrwxrwxrwx 1 root root   36 Feb  3 21:00 vhost2 -> ../sites-available/vhost2

/ etc / nginx / sites-available /

/etc/nginx/sites-available$ ls -als
total 20
4 drwxr-xr-x 2 root root 4096 Feb  3 21:00 .
4 drwxr-xr-x 8 root root 4096 Feb  3 14:26 ..
4 -rw-r--r-- 1 root root 2416 Feb  3 13:24 default
4 -rwxr-xr-x 1 root root  823 Feb  3 13:26 vhost1
4 -rwxr-xr-x 1 root root  856 Feb  3 19:41 vhost2

vhost2

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

        server_name dev.xxx.fr;
        root /var/www/xxx/dev/web;
        index index.html index.php;

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

        location ~ ^/index\.php(/|$) {
                fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
                internal;
        }

        location ~ \.php$ {
                return 404;
        }

        error_log /var/log/nginx/dev-xxx_error.log;
        access_log /var/log/nginx/dev-xxx_access.log;
}

В настоящее время, когда я вызываю мой vhost2 в моем навигаторе, у меня есть:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

Но я только ставлю phpinfo(); по моему index.php. Таким образом, кажется, nginx все еще использует vhost по умолчанию.

Когда я делаю вызов curl для моего vhost, я получаю:

curl -I http://dev.xxx.fr

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Mon, 03 Feb 2020 17:05:49 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 25 Jan 2020 17:25:19 GMT
Connection: keep-alive
ETag: "5e2c79ff-264"
Accept-Ranges: bytes

И без параметров -I:

curl http://dev.xxx.fr
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Да, даже если я удалю символическую ссылку, а затем перезапустите nginx, я все равно получу html стандартного vhost :(.

Я установил lsof, затем запустите sudo lsof -i:80:

/etc/nginx/sites-available$ sudo lsof -i:80
COMMAND  PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   9759     root   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9759     root   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9770 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9770 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9771 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9771 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9772 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9772 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9773 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9773 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9774 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9774 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9775 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9775 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9776 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9776 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9777 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9777 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)

Когда я пытаюсь использовать порт 80, в следующем выводе journalctl -xe после попытки перезагрузить nginx:

Feb 03 19:41:21 ns3017362 nginx[6303]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Feb 03 19:41:21 ns3017362 nginx[6303]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Feb 03 19:41:21 ns3017362 nginx[6303]: nginx: [emerg] still could not bind()
Feb 03 19:41:21 ns3017362 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
-- Subject: Unit process exited
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- An ExecStart= process belonging to unit nginx.service has exited.
--
-- The process' exit code is 'exited' and its exit status is 1.
Feb 03 19:41:21 ns3017362 systemd[1]: nginx.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- The unit nginx.service has entered the 'failed' state with result 'exit-code'.
Feb 03 19:41:21 ns3017362 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: A start job for unit nginx.service has failed
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- A start job for unit nginx.service has finished with a failure.
--
-- The job identifier is 91291 and the job result is failed.
Feb 03 19:41:21 ns3017362 sudo[6295]: pam_unix(sudo:session): session closed for user root
Feb 03 19:41:35 ns3017362 sudo[6308]: xxx : TTY=pts/0 ; PWD=/etc/nginx/sites-enabled ; USER=root ; COMMAND=/usr/bin/journalctl -xe
Feb 03 19:41:35 ns3017362 sudo[6308]: pam_unix(sudo:session): session opened for user root by xxx(uid=0)
Feb 03 19:41:41 ns3017362 sudo[6308]: pam_unix(sudo:session): session closed for user root
Feb 03 19:41:42 ns3017362 sudo[6313]: xxx : TTY=pts/0 ; PWD=/etc/nginx/sites-enabled ; USER=root ; COMMAND=/usr/bin/journalctl -xe
Feb 03 19:41:42 ns3017362 sudo[6313]: pam_unix(sudo:session): session opened for user root by xxx(uid=0)

I попробуйте команду nginx -T, которую вы мне дали:

/etc/nginx/sites-available$ sudo nginx -T

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

# configuration file /etc/nginx/modules-enabled/50-mod-http-auth-pam.conf:
load_module modules/ngx_http_auth_pam_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-dav-ext.conf:
load_module modules/ngx_http_dav_ext_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-echo.conf:
load_module modules/ngx_http_echo_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-geoip.conf:
load_module modules/ngx_http_geoip_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-image-filter.conf:
load_module modules/ngx_http_image_filter_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-subs-filter.conf:
load_module modules/ngx_http_subs_filter_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-upstream-fair.conf:
load_module modules/ngx_http_upstream_fair_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-http-xslt-filter.conf:
load_module modules/ngx_http_xslt_filter_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-mail.conf:
load_module modules/ngx_mail_module.so;

# configuration file /etc/nginx/modules-enabled/50-mod-stream.conf:
load_module modules/ngx_stream_module.so;

# configuration file /etc/nginx/mime.types:

types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/javascript                js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;

    text/mathml                           mml;
    text/plain                            txt;
    text/vnd.sun.j2me.app-descriptor      jad;
    text/vnd.wap.wml                      wml;
    text/x-component                      htc;

    image/png                             png;
    image/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg svgz;
    image/webp                            webp;

    application/font-woff                 woff;
    application/java-archive              jar war ear;
    application/json                      json;
    application/mac-binhex40              hqx;
    application/msword                    doc;
    application/pdf                       pdf;
    application/postscript                ps eps ai;
    application/rtf                       rtf;
    application/vnd.apple.mpegurl         m3u8;
    application/vnd.ms-excel              xls;
    application/vnd.ms-fontobject         eot;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    application/x-7z-compressed           7z;
    application/x-cocoa                   cco;
    application/x-java-archive-diff       jardiff;
    application/x-java-jnlp-file          jnlp;
    application/x-makeself                run;
    application/x-perl                    pl pm;
    application/x-pilot                   prc pdb;
    application/x-rar-compressed          rar;
    application/x-redhat-package-manager  rpm;
    application/x-sea                     sea;
    application/x-shockwave-flash         swf;
    application/x-stuffit                 sit;
    application/x-tcl                     tcl tk;
    application/x-x509-ca-cert            der pem crt;
    application/x-xpinstall               xpi;
    application/xhtml+xml                 xhtml;
    application/xspf+xml                  xspf;
    application/zip                       zip;

    application/octet-stream              bin exe dll;
    application/octet-stream              deb;
    application/octet-stream              dmg;
    application/octet-stream              iso img;
    application/octet-stream              msi msp msm;

    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

    audio/midi                            mid midi kar;
    audio/mpeg                            mp3;
    audio/ogg                             ogg;
    audio/x-m4a                           m4a;
    audio/x-realaudio                     ra;

    video/3gpp                            3gpp 3gp;
    video/mp2t                            ts;
    video/mp4                             mp4;
    video/mpeg                            mpeg mpg;
    video/quicktime                       mov;
    video/webm                            webm;
    video/x-flv                           flv;
    video/x-m4v                           m4v;
    video/x-mng                           mng;
    video/x-ms-asf                        asx asf;
    video/x-ms-wmv                        wmv;
    video/x-msvideo                       avi;
}

# configuration file /etc/nginx/sites-enabled/vhost2:
server {
        listen 8080;
        listen [::]:8080;

        server_name dev.xxx.fr;
        root /var/www/xxx/dev/web;
        index index.html index.php;

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

        location ~ ^/index\.php(/|$) {
                fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
                internal;
        }

        location ~ \.php$ {
                return 404;
        }

        error_log /var/log/nginx/dev-xxx_error.log;
        access_log /var/log/nginx/dev-xxx_access.log;
}

# configuration file /etc/nginx/fastcgi_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

# configuration file /etc/nginx/sites-enabled/vhost1:
server {
        listen 8080;
        listen [::]:8080;

        server_name dev.yyy.fr;
        root /var/www/yyy/dev/public;
        index index.html index.php;

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

        location ~ ^/index\.php(/|$) {
                fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $realpath_root;
                internal;
        }

        location ~ \.php$ {
                return 404;
        }

        error_log /var/log/nginx/dev-yyy_error.log;
        access_log /var/log/nginx/dev-yyy_access.log;
}

Решение

Я уничтожаю процесс на порту 80 следующим образом:

sudo lsof -i:80

COMMAND  PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   9759     root   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9759     root   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9770 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9770 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9771 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9771 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9772 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9772 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9773 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9773 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9774 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9774 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9775 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9775 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9776 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9776 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
nginx   9777 www-data   14u  IPv4 2001766      0t0  TCP *:http (LISTEN)
nginx   9777 www-data   15u  IPv6 2001767      0t0  TCP *:http (LISTEN)
sudo kill 9759

Затем я обновляю все мои vhosts для использования порта 80 и перезагружаюсь nginx. Он работает правильно.

Спасибо всем за помощь и советы (команды) :).

...