apache+nginx基本配置

来源:互联网 发布:俄罗斯现状 知乎 编辑:程序博客网 时间:2024/05/21 12:39

apache基本配置

在Ubuntu的apache的配置文件是 /etc/apache2/apache2.conf,apache在启动时会自动读取这个文件的配置信息。而其他的一些配置文件,则是通过Include加载的。

# Include module configuration:IncludeOptional mods-enabled/*.loadIncludeOptional mods-enabled/*.conf# Include list of ports to listen onInclude ports.conf......# Include generic snippets of statementsIncludeOptional conf-enabled/*.conf# Include the virtual host configurations:IncludeOptional sites-enabled/*.conf

ports.conf,用于设置Apache监听的端口。

Listen 80Listen 8080

虚拟主机配置
/etc/apache2/sites-enabled/test.conf

# 设置虚拟主机<VirtualHost *:80>    # 网站名称    ServerName www.test.com    # 如果有多个网站名称,可以加在ServerAlias后加上其他网站别名。    # 别名间以空格隔开。    ServerAlias www.test.com test.com    ServerAdmin webmaster@localhost    #网站根目录    DocumentRoot /var/www/html    #网站首页面    DirectoryIndex index.php          #目录权限设置       <Directory /mnt/workspace/web/wordblog/>          Options Indexes FollowSymLinks          #忽略.htaccess          AllowOverride None          #允许所有请求访问资源          Require all granted    </Directory>    ......</VirtualHost>

当用ls 查看sites-enabled目录时,发现存放的是指向sites-available目录文件的符号链接。所以 /etc/apache2/sites-available目录内才是真正的配置文件。

这样的好处是:
如果apache上配置了多个虚拟主机,每个虚拟主机的配置文件都放在 sites-available下,那么对于虚拟主机的停用、启用就非常方便了。当在sites-enabled下建立一个指向某个虚拟主机配置文件的链接时,就启用了它;如果要关闭某个虚拟主机的话,只需删除相应的链接即可,就不需要去改配置文件了。

启用配置
sudo ln -s /etc/apache2/sites-available/test.conf /etc/apache2/sites-enabled/test.conf

重启apache
sudo /etc/init.d/apache2 restart

nginx基本配置

nginx的配置在/etc/nginx/nginx.conf,也是使用include包含了服务器的配置信息。

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

nginx和apache配置文件的目录结构几乎一样,sites-enabled和sites-available目录。

反向代理
/etc/nginx/sites-enabled/test.conf

#反向代理upstream backend_http {    #ip_hash;    server 192.168.1.100:8089;    server 127.0.0.1:8089;}server {        listen 80 default_server;        listen [::]:80 default_server;        #网站的根目录        root /var/www/html;        #网站默认页面        index index.html index.php;        #网站名称        server_name www.test.com;        location ~ \.php$ {            #设置主机头和客户端真实地址,以便服务器获取客户端真实IP            proxy_set_header Host $host;                                proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             #禁用缓存             proxy_buffering off;             #反向代理的地址             proxy_pass http://backend_http;             }}

同apache一样,如果需要在nginx上配置多个虚拟主机,在 sites-available下创建配置文件。在sites-enabled下建立一个指向某个虚拟主机配置文件的链接,就启用了它;如果要关闭某个虚拟主机的话,只需删除相应的链接即可,就不需要去改配置文件了。

重启nginx
sudo /etc/init.d/nginx restart