【nginx】中server配置说明

本文详细介绍了Nginx服务器的配置方法,包括监听端口、域名设置、静态文件根目录、首页索引文件、错误页面配置、FastCGI代理设置等关键信息。同时,文章还提供了如何重启Nginx服务及验证配置正确性的方法。
server {
  listen       80;  //监听的端口号
  server_name  localhost;  //用域名方式访问的地址

  #charset koi8-r; //编码
  #access_log  /var/log/nginx/host.access.log  main;  //访问日志文件和名称

  location / {
      root   /usr/share/nginx/html;  //静态文件根目录
      index  index.html index.htm;  //首页的索引文件
  }

  #error_page  404              /404.html;  //指定错误页面

  # redirect server error pages to the static page /50x.html
  # 把后台错误重定向到静态的50x.html页面
  error_page   500 502 503 504  /50x.html; 
   当路径是/50x.html去找/usr/share/nginx/html里面的文件
  location = /50x.html {
      root   /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  如果访问的是.php结尾会把请求转发给http://127.0.0.1;
  # 代理PHP脚本到80端口上的apache服务器
  值Apache服务器
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  # 把PHP脚本9000端口上监听的FastCGI服务
  执行时PHP服务器
  如果访问的是.php结尾会把请求转发给127.0.0.1:9000;
  #location ~ \.php$ {
  #    root           html;
  #    fastcgi_pass   127.0.0.1:9000;
  #    fastcgi_index  index.php;
  #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  #    include        fastcgi_params;
  #}

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  # 不允许访问.htaccess文件
  #location ~ /\.ht {
  #    deny  all; 禁止所以人访问
  #}
}

// 重启
nginx -s reload
systenmctl reload nginx.service

// 验证
nginx -t

// 多级代理
http_x_forwarded_for 客户端代理IP1 代理IP2

  

转载于:https://www.cnblogs.com/wuxianqiang/p/10705212.html

<think>好的,用户现在想知道Nginxserver配置块的位置,特别是与伪静态URL配置相关的地方。我需要结合之前的对话和提供的引用内容来回答这个问题。 首先,回顾之前的回答,用户之前询问了如何实现伪静态URL,并提到了Apache和Nginx配置方法。在引用内容中,用户提供的引用[2]和[3]都涉及Nginx配置示例,其中引用[2]展示了一个server块的配置,里面包含了rewrite规则。这可能意味着用户现在需要更具体地了解server块的存放位置,以便正确配置伪静态URL。 接下来,我需要确定用户的需求层次。用户可能正在尝试配置Nginx的伪静态规则,但不确定在哪里编写这些配置。他们可能对Nginx配置文件结构不太熟悉,需要导如何找到或创建server块的位置。 根据Nginx的常规配置结构,主配置文件通常是nginx.conf,而server块通常位于sites-available目录下的独立文件中,然后通过符号链接到sites-enabled目录。此外,用户可能也会在conf.d目录下放置配置片段。需要明确这些路径,并说明如何编辑和测试配置。 同时,考虑到用户之前提到的伪静态配置,需要强调在server块内的location部分添加rewrite规则。例如,引用[2]中的例子就是在server块的location / {}部分配置了rewrite。这可能帮助用户理解具体的配置位置。 另外,用户可能需要知道如何检查配置文件的正确性,比如使用nginx -t命令,以及如何重新加载配置而不中断服务。这些都是实际操作中的重要步骤,需要包含在回答中。 还要注意用户可能遇到的常见问题,比如配置未生效,可能由于路径错误、权限问题或缓存未清除。在回答中提供这些注意事项可以帮助用户排查问题。 最后,确保回答结构清晰,分步骤说明,并给出具体的示例,这样用户能够一步步跟随操作。同时,生成的相关问题应该覆盖用户可能的后续疑问,比如如何测试配置、优化性能等。</think>#### 一、Nginx中`server`配置块的标准位置 在Nginx配置体系中,`server`配置块主要用于定义虚拟主机(网站)的行为,包含伪静态URL重写(`rewrite`)等核心配置。其存放位置主要有以下三类: --- ##### 1. **主配置文件** - **路径**: `/etc/nginx/nginx.conf` - **作用**: 全局配置入口,可直接在`http {}`块内编写`server {}`配置 - **示例**: ```nginx http { server { listen 80; server_name example.com; location / { rewrite ^/article-(\d+)\.html$ /article.php?id=$1 last; } } } ``` --- ##### 2. **独立配置文件** - **推荐目录**: - `/etc/nginx/sites-available/`: 存储可用配置(需手动启用) - `/etc/nginx/sites-enabled/`: 通过符号链接激活配置 - `/etc/nginx/conf.d/*.conf`: 自动加载的配置片段 - **操作流程**: ```bash # 创建配置文件 sudo vim /etc/nginx/sites-available/my_site.conf # 添加伪静态规则(示例) server { listen 80; server_name myapp.com; location / { rewrite ^/product/(\w+)/?$ /product.php?name=$1; } } # 启用配置 sudo ln -s /etc/nginx/sites-available/my_site.conf /etc/nginx/sites-enabled/ # 测试并重载 sudo nginx -t && sudo systemctl reload nginx ``` --- ##### 3. **动态模块引入** - **场景**: 使用`include`令动态加载外部配置 - **示例**: ```nginx http { include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } ``` --- #### 二、伪静态URL配置的核心区域 在`server {}`块中,伪静态规则主要通过**`location {}`块**和**`rewrite`令**实现: ```nginx server { listen 80; server_name example.org; # 伪静态规则定义区域 location / { # 将 /blog-123.html 映射到 /blog.php?id=123 rewrite ^/blog-(\d+)\.html$ /blog.php?id=$1 last; # 其他请求转发(如PHP处理) try_files $uri $uri/ /index.php?$query_string; } # 静态资源处理 location ~* \.(jpg|css|js)$ { root /var/www/static; expires 30d; } } ``` --- #### 三、配置文件验证与调试 1. **语法检查**: ```bash sudo nginx -t # 输出"configuration is ok"表示无语法错误 ``` 2. **日志追踪**: - 错误日志路径: `/var/log/nginx/error.log` - 调试伪静态匹配: ```nginx rewrite_log on; # 在配置中启用重写日志 ``` --- #### 四、注意事项 1. **优先级问题**: - 当多个`server`块匹配同一域名时,按`listen`令的`default_server`标记和配置文件加载顺序决定优先级[^1] 2. **正则冲突**: - 避免在`location`块中使用过于宽泛的正则(如`location ~ .*`),可能覆盖伪静态规则 3. **性能影响**: - 复杂`rewrite`规则建议放在`server`块顶层而非`location`内部,减少匹配次数[^2] --- #### 五、典型应用场景 | 场景 | 配置示例 | |---------------------|-------------------------------------------| | 文章详情页伪静态 | `rewrite ^/post/(\d+)/?$ /post.php?id=$1` | | 分类页参数简化 | `rewrite ^/category/(\w+)$ /cat.php?name=$1` | | 多语言URL支持 | `rewrite ^/(en|fr)/about$ /about.php?lang=$1` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值