域重定向到nginx中的错误站点,配置文件中的多个站点

前端之家收集整理的这篇文章主要介绍了域重定向到nginx中的错误站点,配置文件中的多个站点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在Nginx上设置了2个网站,正确的应该根据访问的域加载.

例如:
website1.com
website2.com

但是,当访问这些域中的任何一个时,它们都会加载website1.com.

我的配置出了什么问题?两个配置都在启用站点的单独文件

website1.com配置

  1. server {
  2. listen 80;
  3. listen [::]:80 ipv6only=on;
  4. root /var/www/website1.com;
  5. index index.PHP index.html index.htm;
  6. # Make site accessible from http://localhost/
  7. server_name website1.com;
  8. location / {
  9. # First attempt to serve request as file,then
  10. # as directory,then fall back to displaying a 404.
  11. try_files $uri $uri/ /index.PHP?$query_string;
  12. # Uncomment to enable naxsi on this location
  13. # include /etc/Nginx/naxsi.rules
  14. }
  15. location ~ \.PHP${
  16. try_files $uri /index.PHP =404;
  17. fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  18. fastcgi_pass unix:/var/run/PHP5-fpm.sock;
  19. fastcgi_index index.PHP;
  20. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  21. include fastcgi_params;
  22. }
  23. # Only for Nginx-naxsi used with Nginx-naxsi-ui : process denied requests
  24. #location /RequestDenied {
  25. # proxy_pass http://127.0.0.1:8080;
  26. #}
  27. #error_page 404 /404.html;
  28. # redirect server error pages to the static page /50x.html
  29. #
  30. #error_page 500 502 503 504 /50x.html;
  31. #location = /50x.html {
  32. # root /usr/share/Nginx/html;
  33. #}
  34. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  35. #
  36. #location ~ \.PHP${
  37. # fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  38. # # NOTE: You should have "cgi.fix_pathinfo = 0;" in PHP.ini
  39. #
  40. # # With PHP5-cgi alone:
  41. # fastcgi_pass 127.0.0.1:9000;
  42. # # With PHP5-fpm:
  43. # fastcgi_pass unix:/var/run/PHP5-fpm.sock;
  44. # fastcgi_index index.PHP;
  45. # include fastcgi_params;
  46. #}
  47. # deny access to .htaccess files,if Apache's document root
  48. # concurs with Nginx's one
  49. #
  50. #location ~ /\.ht {
  51. # deny all;
  52. #}
  53. }

website2.com配置

  1. server {
  2. listen 80;
  3. listen [::]:80 ipv6only=on;
  4. root /var/www/website2.com;
  5. index index.PHP index.html index.htm;
  6. # Make site accessible from http://localhost/
  7. server_name website2.com;
  8. location / {
  9. # First attempt to serve request as file,then fall back to displaying a 404.
  10. try_files $uri $uri/ /index.PHP?$query_string;
  11. # Uncomment to enable naxsi on this location
  12. # include /etc/Nginx/naxsi.rules
  13. }
  14. location ~ \.PHP${
  15. try_files $uri /index.PHP =404;
  16. fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  17. fastcgi_pass unix:/var/run/PHP5-fpm.sock;
  18. fastcgi_index index.PHP;
  19. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  20. include fastcgi_params;
  21. }
  22. # Only for Nginx-naxsi used with Nginx-naxsi-ui : process denied requests
  23. #location /RequestDenied {
  24. # proxy_pass http://127.0.0.1:8080;
  25. #}
  26. #error_page 404 /404.html;
  27. # redirect server error pages to the static page /50x.html
  28. #
  29. #error_page 500 502 503 504 /50x.html;
  30. #location = /50x.html {
  31. # root /usr/share/Nginx/html;
  32. #}
  33. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  34. #
  35. #location ~ \.PHP${
  36. # fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  37. # # NOTE: You should have "cgi.fix_pathinfo = 0;" in PHP.ini
  38. #
  39. # # With PHP5-cgi alone:
  40. # fastcgi_pass 127.0.0.1:9000;
  41. # # With PHP5-fpm:
  42. # fastcgi_pass unix:/var/run/PHP5-fpm.sock;
  43. # fastcgi_index index.PHP;
  44. # include fastcgi_params;
  45. #}
  46. # deny access to .htaccess files,if Apache's document root
  47. # concurs with Nginx's one
  48. #
  49. #location ~ /\.ht {
  50. # deny all;
  51. #}
  52. }

Nginx.conf

  1. user www-data;
  2. worker_processes 4;
  3. pid /run/Nginx.pid;
  4. events {
  5. worker_connections 4096;
  6. multi_accept on;
  7. }
  8. http {
  9. ##
  10. # Basic Settings
  11. ##
  12. #sendfile on;
  13. #tcp_nopush on;
  14. #tcp_nodelay on;
  15. #keepalive_timeout 10;
  16. types_hash_max_size 2048;
  17. server_tokens off;
  18. # server_names_hash_bucket_size 64;
  19. # server_name_in_redirect off;
  20. include /etc/Nginx/mime.types;
  21. default_type application/octet-stream;
  22. add_header X-Frame-Options SAMEORIGIN;
  23. add_header X-Content-Type-Options nosniff;
  24. add_header X-XSS-Protection "1; mode=block";
  25. ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
  26. ssl_prefer_server_ciphers on;
  27. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  28. ##
  29. # Logging Settings
  30. ##
  31. access_log off;
  32. log_not_found off;
  33. error_log /var/log/Nginx-error.log warn;
  34. #access_log /var/log/Nginx/access.log;
  35. #error_log /var/log/Nginx/error.log;
  36. ##
  37. # Gzip Settings
  38. ##
  39. gzip on;
  40. gzip_disable "msie6";
  41. gzip_vary on;
  42. gzip_proxied any;
  43. gzip_comp_level 6;
  44. gzip_min_length 1100;
  45. gzip_buffers 16 8k;
  46. gzip_http_version 1.1;
  47. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+RSS text/javascript;
  48. # gzip_vary on;
  49. # gzip_proxied any;
  50. # gzip_comp_level 6;
  51. # gzip_buffers 16 8k;
  52. # gzip_http_version 1.1;
  53. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+RSS text/javascript;
  54. ##
  55. # Nginx-naxsi config
  56. ##
  57. # Uncomment it if you installed Nginx-naxsi
  58. ##
  59. #include /etc/Nginx/naxsi_core.rules;
  60. ##
  61. # Nginx-passenger config
  62. ##
  63. # Uncomment it if you installed Nginx-passenger
  64. ##
  65. #passenger_root /usr;
  66. #passenger_ruby /usr/bin/ruby;
  67. ##
  68. # Virtual Host Configs
  69. ##
  70. include /etc/Nginx/conf.d/*.conf;
  71. include /etc/Nginx/sites-enabled/*;
  72. open_file_cache max=2000 inactive=20s;
  73. open_file_cache_valid 60s;
  74. open_file_cache_min_uses 5;
  75. open_file_cache_errors off;
  76. client_max_body_size 50M;
  77. client_body_buffer_size 1m;
  78. client_body_timeout 15;
  79. client_header_timeout 15;
  80. keepalive_timeout 2 2;
  81. send_timeout 15;
  82. sendfile on;
  83. tcp_nopush on;
  84. tcp_nodelay on;
  85. fastcgi_buffers 256 16k;
  86. fastcgi_buffer_size 128k;
  87. fastcgi_connect_timeout 3s;
  88. fastcgi_send_timeout 120s;
  89. fastcgi_read_timeout 120s;
  90. fastcgi_busy_buffers_size 256k;
  91. fastcgi_temp_file_write_size 256k;
  92. reset_timedout_connection on;
  93. server_names_hash_bucket_size 100;
  94. }
  95. #mail {
  96. # # See sample authentication script at:
  97. # # http://wiki.Nginx.org/ImapAuthenticateWithApachePHPScript
  98. #
  99. # # auth_http localhost/auth.PHP;
  100. # # pop3_capabilities "TOP" "USER";
  101. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  102. #
  103. # server {
  104. # listen localhost:110;
  105. # protocol pop3;
  106. # proxy on;
  107. # }
  108. #
  109. # server {
  110. # listen localhost:143;
  111. # protocol imap;
  112. # proxy on;
  113. # }
  114. #}
最佳答案
如果没有默认站点配置,那么Nginx将在显示未经过专门配置的请求时自动选择第一个可用的配置文件.

在这种情况下,当您在任一域之前添加www时,Nginx正在选择website1.com配置

解决此特定问题,请替换以下配置行

website1.com配置

  1. server_name website1.com www.website1.com;

website2.com配置

  1. server_name website2.com www.website2.com;

您也可以使用* .website1.com或* .website2.com来捕获所有请求.

你应该有一个默认的网站配置,因为有一些像这样的怪癖,可以避免一个.即使它只是返回400错误.

猜你在找的Nginx相关文章