我刚刚购买了一个域名 (example.com),并设置了 URL 框架重定向到我的服务器 IP XXX.XXX.XXX.XXX,但我收到了 403 禁止错误。我可以通过服务器 IP 正常访问页面。我想要的是使用我的域名复制服务器 IP 的行为,因此我尝试复制我的 IP 的虚拟主机,但它不起作用。
这是我的虚拟主机文件:
server {
listen 80;
server_name example.com
;
access_log /var/log/nginx/website.access_log;
error_log /var/log/nginx/website.error_log;
root /var/www;
index index.html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/phpMyAdmin;
include fastcgi_params;
}
}
我当前使用的方法是直接通过 IP 访问服务器(有效):
server {
listen 80;
server_name xxx.xxx.xxx.xxx
;
access_log /var/log/nginx/website.access_log;
error_log /var/log/nginx/website.error_log;
root /var/www;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/phpMyAdmin;
include fastcgi_params;
}
}
这是我的 nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
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;
autoindex off;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# 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/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/default;
include /etc/nginx/sites-enabled/phpMyAdmin;
include /etc/nginx/sites-enabled/example.com;
}
我也尝试过:
sudo chown -R www-data:www-data /var/www
sudo chmod -R 775 /var/www
任何建议都将不胜感激。我已经研究这个一段时间了。
谢谢!
编辑 - - -
问题出在我的域名注册商,我将域名设置为“URL 转发”,而它应该是“A 记录”。这个帮我解决了这个问题。祝大家好运!
答案1
我几乎有同样的问题,但我用这个配置解决了它如果你的服务器上只有一个站点,那么你必须删除默认配置文件。rm /etc/nginx/sites-available/default 只保留 /etc/nginx/sites-available/example.com 并使用此配置
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/website.access_log;
error_log /var/log/nginx/website.error_log;
root /var/www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/phpMyAdmin;
include fastcgi_params;
}
}
如果你希望网站也能通过 IP 被接受,那么你应该创建默认配置文件
编辑 /etc/nginx/sites-available/default 并使用您第一次使用的相同配置
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
root /var/www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/phpMyAdmin;
include fastcgi_params;
}
}
最后,你可以将你的站点 example.com 文件移动到 /var/www/example.com 并删除默认配置文件,然后重新启动 nginx