1、nginx怎麼把兩個一級域名放在一個伺服器上 急需要解答
這個只需要在nginx的配置中配置兩個server就可以了。這兩個server監聽同一個埠,但是server_name不同。如下所示:
server {如果有多個server的話,一定要確保設置其中一個為default_server,以保證,如果nginx按照server_name匹配不到合適的server時,可以把請求轉發給默認的server處理。
2、nginx怎麼把所有請求都轉到一個域名
server {
listen 80;
server_name 目前的域名;
rewrite ^/(.*) 要轉到的域名/$1 permanent;
}
3、nginx如何配置域名
方法一:多個.conf方法(優點是靈活,缺點就是站點比較多配置起來麻煩)
這里以配置2個站點(2個域名)為例,n 個站點可以相應增加調整,假設:
IP地址: 192.168.1.100
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2
配置 nginx virtual hosting 的基本思路和步驟如下:
把2個站點 example1.com, example2.com 放到 nginx 可以訪問的目錄 /www/
給每個站點分別創建一個 nginx 配置文件 example1.com.conf,example2.com.conf, 並把配置文件放到 /usr/local/nginx/vhosts/
然後在 /usr/local/nginx/nginx.conf 裡面加一句 include 把步驟2創建的配置文件全部包含進來(用 * 號)
重啟 nginx
1、打開 /usr/local/nginx/nginix.conf 文件,在相應位置加入 include 把以上2個文件包含進來
user www www;
worker_processes 1;
# main server error log
error_log /usr/local/nginx/log/nginx/error.log ;
pid /usr/local/nginx/nginx.pid;
events {
worker_connections 51200;
}
# main server config
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」『;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name _;
access_log /usr/local/nginx/log/nginx/access.log main;
server_name_in_redirect off;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
# 包含所有的虛擬主機的配置文件
include /usr/local/nginx/vhosts/*;
}
2、在 /usr/local/nginx 下創建 vhosts 目錄
mkdir /usr/local/nginx/vhosts
3、在 /usr/local/nginx/vhosts/ 里創建一個名字為 example1.com.conf 的文件,把以下內容拷進去
server {
listen 80;
server_name example1.com www. example1.com;
access_log /www/access_ example1.log main;
location / {
root /www/example1.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
3、在 /usr/local/nginx/vhosts/ 里創建一個名字為 example2.com.conf 的文件,把以下內容拷進去
server {
listen 80;
server_name example2.com www. example2.com;
access_log /www/access_ example1.log main;
location / {
root /www/example2.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht {
deny all;
}
}
5、重啟 Nginx
/etc/init.d/nginx restart
方法二:動態目錄方法(優點是方便,每個域名對應一個文件夾,缺點是不靈活)
這個簡單的方法比起為每一個域名建立一個 vhost.conf 配置文件來講,只需要在現有的配置文件中增加如下內容:
# Replace this port with the right one for your requirements
# 根據你的需求改變此埠
listen 80; #could also be 1.2.3.4:80 也可以是1.2.3.4:80的形式
# Multiple hostnames seperated by spaces. Replace these as well.
# 多個主機名可以用空格隔開,當然這個信息也是需要按照你的需求而改變的。
server_name star.yourdomain.com *.yourdomain.com http://www.*.yourdomain.com/;
#Alternately: _ *
#或者可以使用:_ * (具體內容參見本維基其他頁面)
root /PATH/TO/WEBROOT/$host;
error_page 404 http://yourdomain.com/errors/404.html;
access_log logs/star.yourdomain.com.access.log;
location / {
root /PATH/TO/WEBROOT/$host/;
index index.php;
}
# serve static files directly
# 直接支持靜態文件 (從配置上看來不是直接支持啊)
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires 30d;
}
location ~ .php$ {
# By all means use a different server for the fcgi processes if you need to
# 如果需要,你可以為不同的FCGI進程設置不同的服務信息
fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
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_intercept_errors on;
}
location ~ /.ht {
deny all;
}
最後附另外一個二級域名匹配的方法
綁定域名
server_name *.abcd.com;
獲取主機名
if ( $host ~* (.*).(.*).(.*))
{
set $domain $1;
}
定義目錄
root html/abc/$domain/;
location /
{
root html/abcd/$domain;
index index.html index.php;
4、nginx代理域名伺服器上可以訪問,換外網無法訪問。
先查解析,在本地用ping命令去查一下,看解析的地址,是不是伺服器的地址。如果是,那就是解析沒問題,如果不是,那就是解析的問題。
如果解析沒問題,再查埠,用telnet 域名 80 這個命令去查下,看80埠能不能通。如果可以通,那就是伺服器那邊的事,如果不能通,可能是因為機房攔截。
5、nginx怎麼配置IP和域名都能訪問
一個nginx伺服器只能有一個虛擬主機允許IP訪問
只要在server_name最後面添加一個default,就可以在其他nginx沒有定義的域名下,使用當前server解析(例如,其他server都沒有定義ip地址作為server_name則用IP訪問會被打到default主機上)
6、nginx 文件配置 如何設置域名
1.路徑: /etc/nginx/nginx.conf 和 /etc/nginx/conf.d,
其實只有/etc/nginx/nginx.conf 這一個配置文件,因為在nginx.conf中,其他配置文件都是可以利用 include 指令·引入的
部分配置文件:
server
{
listen 80;
server_name test.net;
root /var/www/test;#include none.conf;
#error_page 404 /404.html;
location ~ [^/].php(/|$)
{
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
client_max_body_size 500m;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 12h;
}
#location = /HBLS.deb {
# rewrite . /HBLS.deb;
# default_type application/x-deb;
#}
access_log off;
#access_log logs/lung.access.log;
#error_log logs/lung.error.log debug;
}
7、nginx如何配置訪問時域名中不帶某字元就強制重定向
location !~ static {
rewrite ^ /error_pages/404.html;
return 404;
}
或者
location / {
if($request_uri !~ "static") {
rewrite ^ /error_pages/404.html;
return 404;
}
}
沒有測試過,自己去測試一下吧,不對的話,再多搜索一下相回關資料答看看吧
8、為什麼我的Linux Nginx添加了新域名新站點,訪問新域名卻是另一個網站
如果你的配置文件沒有錯,你添加的是新的域名的話,有沒有可能是你的域名沒有被正常的dns伺服器解析?
建議檢查一下網卡裡面的DNS=888.888.88.88這個,然後本地nslookup 解析一下你的域名看看,能不能正常的解析出來ip,然後反向解析一下。
如果不是這個問題的話,那就還是nginx的配置文件的邏輯有問題了。
分開來一點一點分析一下,期待你盡快解決。
9、nginx更換域名後圖片不顯示了
1.緩存問題,清一下瀏覽器緩存。
2.看nginx日誌定位,如果是叉叉,應該報404錯誤
10、nginx域名如何定位到項目的某個目錄下
server {
listen 80 default_server;
server_name test.example.com;
root F:/web;
access_log logs/access.log main buffer=1024k;
}